Android:ListView中的Costum布局

时间:2016-02-16 18:13:38

标签: android android-layout listview android-studio

我有一个带有自定义适配器的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>

这是我的问题:

Problem with layout

ImageView始终位于其他视图的旁边,而不是附加到容器的右侧。

如何让ImageView始终位于右侧?

---编辑---

感谢Farham!有效。但现在我还有几个问题 首先是ImageView没有拉伸到View的高度,
第二,ListView中的最后一项是部分显示:

New problem

我无法滚动低于图片中显示的内容...

4 个答案:

答案 0 :(得分:1)

您可能需要使用RelativeLayout并为ImageView设置layout_alignParentRight =“true”。

或者您可以将内容android:layout_width0dp

LinearLayout更改为ImageView
 <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:weightSum="4"
        android:layout_weight="2">

...

 <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/short_data_poster"
        android:layout_gravity="center_vertical|right"
        android:layout_weight="1"/>

答案 1 :(得分:1)

您可以 FrameLayout 替换 RelativeLayout (所有视图的父级),然后添加 ImageView 并设置 layout_alignParentRight =&#34; true&#34;

包含所有其他视图的最顶级的LinearLayout 应始终与relativeLayout的左侧对齐
左侧的imageView。

答案 2 :(得分:0)

您可以使用相对布局,或者如果您想使用线性布局有效地使用权重。如果使用android:weight="2"//or something else,则宽度应为零dp。 android:width="0dp"(对于水平布局和android:height =&#34; 0&#34;如果使用垂直布局)。您的布局应如下所示:

              <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:weightSum="2">

                    <TextView
                        android:layout_width="0dp" //note this
                        android:layout_height="match_parent"
                        android:layout_weight="1"/>
                   <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"/>
</LinearLayout>

答案 3 :(得分:0)

您可以使用线性布局来实现此目的,复制粘贴此代码并查看其是否有效

<?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="0dp"
            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="0dp"
            android:layout_height="match_parent"
            android:id="@+id/short_data_poster"
            android:layout_gravity="center_vertical|right"
            android:layout_weight="1"/>
    </LinearLayout>

</FrameLayout>