我有一个带有自定义适配器的ListView,它使用自定义布局:
package giorag.rottentomatoexampleapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class ShortMovieDataAdapter extends ArrayAdapter<ShortMovie> {
public ShortMovieDataAdapter(Context context, int resource, ShortMovie[] objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ShortMovie movie = getItem(position);
MovieHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.short_movie_data_view, parent, false);
holder = new MovieHolder();
holder.movieTitle = (TextView)convertView.findViewById(R.id.short_data_title);
holder.year = (TextView)convertView.findViewById(R.id.short_data_year);
holder.type = (TextView)convertView.findViewById(R.id.short_data_type);
holder.imdb = (TextView)convertView.findViewById(R.id.short_data_imdb_id);
holder.poster = (ImageView) convertView.findViewById(R.id.short_data_poster);
convertView.setTag(holder);
}
else {
holder = (MovieHolder)convertView.getTag();
}
holder.movieTitle.setText(movie.getTitle());
holder.year .setText(movie.getYear());
holder.type.setText(movie.getType());
holder.imdb.setText(movie.getImdbID());
Picasso.with(getContext()).load(movie.getPoster()).into(holder.poster);
// Return the completed view to render on screen
return convertView;
}
static class MovieHolder {
public TextView movieTitle;
public TextView year;
public TextView type;
public TextView imdb;
public ImageView poster;
}
}
那就是布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:weightSum="4"
android:layout_weight="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/movie_title_placeholder"
android:id="@+id/short_data_title"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="2"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/year_label"
android:id="@+id/textView3"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
android:textIsSelectable="false"
android:singleLine="false"
android:textAlignment="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/year_label"
android:id="@+id/short_data_year"
android:layout_weight="1"
android:layout_gravity="center"
android:phoneNumber="false"
android:textAlignment="center"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="2"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/type_label"
android:id="@+id/textView4"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
android:textIsSelectable="false"
android:singleLine="false"
android:textAlignment="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/type_label"
android:id="@+id/short_data_type"
android:layout_weight="1"
android:layout_gravity="center"
android:phoneNumber="false"
android:textAlignment="center"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="2"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/imdb_id_label"
android:id="@+id/textView2"
android:layout_weight="1"
android:layout_gravity="center"
android:textStyle="bold"
android:textIsSelectable="false"
android:singleLine="false"
android:textAlignment="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/imdb_id_label"
android:id="@+id/short_data_imdb_id"
android:layout_weight="1"
android:layout_gravity="center"
android:phoneNumber="false"
android:textAlignment="center"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/short_data_poster"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"/>
</LinearLayout>
</FrameLayout>
那是我的问题:
ImageView始终位于其余视图的旁边,而不是附加到容器的右侧。
如何使ImageView始终位于右侧?
-编辑-
感谢法拉姆!起作用了。但是现在我还有几个问题。
首先是ImageView没有拉伸到View的高度,
其次,ListView中的最后一项部分显示:
我无法向下滚动到图片中显示的内容...
最佳答案
您可以用RelativeLayout(所有视图的父级)替换FrameLayout,然后添加ImageView并设置layout_alignParentRight =“ true”。
并且包含所有其他视图的最上面的LinearLayout应该始终与relativeLayout的左侧和imageView的左侧对齐。