有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

纵向模式下的java图像不显示在列表视图内部

我试图在我的安卓应用程序中显示一个类似fb的新闻提要(一张带有标语的照片)。为此,我创建了以下代码:

家庭碎片。爪哇

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        newsFeedDescriptions = getResources().getStringArray(R.array.news_feed_description);
        newsFeedPics = getResources().obtainTypedArray(R.array.news_feed_pics);

        newsFeedLayout = (RelativeLayout)rootView.findViewById(R.id.news_feed);
        newsFeedList=(ListView)rootView.findViewById(R.id.list_news_feed);

        newsFeedItems = new ArrayList<NewsFeedItem>();
        newsFeedItems.add(new NewsFeedItem(newsFeedDescriptions[0], newsFeedPics.getResourceId(0, -1)));
        newsFeedItems.add(new NewsFeedItem(newsFeedDescriptions[1], newsFeedPics.getResourceId(1, -1)));
        newsFeedItems.add(new NewsFeedItem(newsFeedDescriptions[2], newsFeedPics.getResourceId(2, -1)));
        newsFeedPics.recycle();
        newsFeedAdapter = new NewsFeedListAdapter(getActivity().getApplicationContext(),newsFeedItems);
        newsFeedList.setAdapter(newsFeedAdapter);
        return rootView;
    }

你回家了。xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:id="@+id/news_feed"
    安卓:background="@color/theme_background">

   <ScrollView
       安卓:layout_width="match_parent"
       安卓:layout_height="487dp" >

    <ListView
        安卓:id="@+id/list_news_feed"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_gravity="start"
        安卓:background="@color/theme_background"
        安卓:divider="@color/list_divider"
        安卓:dividerHeight="1dp" />

    </ScrollView>
</RelativeLayout>

NewsfeedListAdapter。爪哇

package com.example.makemyday;

import java.util.ArrayList;

import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseAdapter;
import 安卓.widget.ImageView;
import 安卓.widget.TextView;


public class NewsFeedListAdapter extends BaseAdapter {

   private Context context;
   private ArrayList<NewsFeedItem> newsFeedItems;

   public NewsFeedListAdapter(Context context, ArrayList<NewsFeedItem> newsFeedItems){
       this.context = context;
       this.newsFeedItems = newsFeedItems;
   }

   @Override
   public int getCount() {
       return newsFeedItems.size();
   }

   @Override
   public Object getItem(int position) {       
       return newsFeedItems.get(position);
   }

   @Override
   public long getItemId(int position) {
       return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView == null) {
           LayoutInflater mInflater = (LayoutInflater)
                   context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
           convertView = mInflater.inflate(R.layout.newsfeed_item, null);
       }

       ImageView img = (ImageView) convertView.findViewById(R.id.newsfeed_image);
       TextView description = (TextView) convertView.findViewById(R.id.newsfeed_description);

       img.setImageResource(newsFeedItems.get(position).getImage());        
       description.setText(newsFeedItems.get(position).getDescription());

       // displaying count
       // check whether it set visible or not

       return convertView;
   }
}

新闻提要。爪哇

public class NewsFeedItem {

    private String description;
    private int image;

    public NewsFeedItem(){}

    public NewsFeedItem(String description, int image){
        this.description = description;
        this.image = image;
    }

    public String getDescription(){
        return this.description;
    }

    public int getImage(){
        return this.image;
    }

    public void setDescription(String description){
        this.description = description;
    }

    public void setImage(int image){
        this.image = image;
    }
}

newsfeed_矩形。xml

<shape xmlns:安卓="http://schemas.安卓.com/apk/res/安卓">
         <gradient
            安卓:endColor="@color/theme_foreground"
            安卓:centerColor="@color/theme_foreground"
            安卓:startColor="@color/theme_foreground"
            安卓:angle="270" />
        <corners
            安卓:radius="3dp" />
        <padding
            安卓:left="10dp"
            安卓:top="10dp"
            安卓:right="10dp"
            安卓:bottom="10dp" />
    </shape>

新闻提要。xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="48dp"
    安卓:background="@drawable/newsfeed_rectangle">

    <ImageView
        安卓:id="@+id/newsfeed_image"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentLeft="true"
        安卓:layout_marginLeft="12dp"
        安卓:layout_marginRight="12dp"
        安卓:layout_centerVertical="true" />

    <TextView
        安卓:id="@+id/newsfeed_description"
        安卓:layout_width="wrap_content"
        安卓:layout_height="match_parent"
        安卓:layout_below="@+id/newsfeed_image"
        安卓:minHeight="12dp"
        安卓:textColor="@color/blue"
        安卓:gravity="center_vertical"
        安卓:paddingRight="40dp"/>

</RelativeLayout>

只有风景中的照片才能正确显示,但在potrait模式下拍摄的照片不能正确显示。。我不明白为什么


共 (1) 个答案

  1. # 1 楼答案

    问题在于您的图像视图。您正在为imageview使用,但尚未设置最小-最大高度-宽度

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    

    所以,在这种情况下,第一个图像可能是大的,第二个图像可能是小的,以此类推。。当你设置“匹配父母”时,这种情况会在你的视野中蔓延吗

    试试这个

     <ImageView
            android:id="@+id/newsfeed_image"
            android:layout_width="wrap_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:minHeight="120dp" // set your value as per your requirement 
            android:minWidth="180dp"  // set your value as per your requirement 
            android:maxWidth="420dp"  // set your value as per your requirement 
            android:maxHeight="680dp"  // set your value as per your requirement 
            android:scaleType="fitXY" // to set image in fix X..Y points 
            android:layout_centerVertical="true" />
    

    希望这对你有用