我正在尝试在我的Android应用程序中显示一个像新闻源一样的fb(一张带标语的照片)。为此,我创建了以下代码:
HomeFragment.java
@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;
}
Fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/news_feed"
android:background="@color/theme_background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="487dp" >
<ListView
android:id="@+id/list_news_feed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="@color/theme_background"
android:divider="@color/list_divider"
android:dividerHeight="1dp" />
</ScrollView>
</RelativeLayout>
NewsfeedListAdapter.java
package com.example.makemyday;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.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;
}
}
NewsfeedItem.java
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_rectangle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="@color/theme_foreground"
android:centerColor="@color/theme_foreground"
android:startColor="@color/theme_foreground"
android:angle="270" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
newsfeeditem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/newsfeed_rectangle">
<ImageView
android:id="@+id/newsfeed_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/newsfeed_description"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/newsfeed_image"
android:minHeight="12dp"
android:textColor="@color/blue"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
</RelativeLayout>
只有风景照片能正常显示,而不是以potrait模式拍摄的照片..我不明白为什么
答案 0 :(得分:0)
问题在于您的图片视图。您正在使用图像视图,但尚未设置最小 - 最大高度 - 宽度。
android:layout_width="match_parent"
android:layout_height="wrap_content"
所以,在这种情况下,第一张图片可能很大,或者第二张图片可能很小,等等......当你设置了“match_parent”时,它会在你的视图中传播。
试试这个,
<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" />
希望这适合你。