【问题标题】:Android: Costum layout in ListViewAndroid:ListView 中的自定义布局
【发布时间】:2016-02-16 18:13:37
【问题描述】:

我有一个带有自定义适配器的 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 中的最后一项正在部分显示:

我无法滚动到低于图像中显示的位置...

【问题讨论】:

    标签: android android-layout listview android-studio


    【解决方案1】:

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

    最顶层的 LinearLayout 包含所有其他视图,应该始终与 relativeLayout 的左侧对齐imageView 的左侧.

    【讨论】:

    • 非常感谢法哈德!那行得通,但现在我有另一个问题。 ImageView 没有拉伸到视图的整个高度,而且,我以前也有过,项目视图中的最后一项只是部分显示。我现在要发布一张新图片
    【解决方案2】:

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

    或者您可以将内部LinearLayoutImageViewandroid:layout_width 更改为0dp

     <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"/>
    

    【讨论】:

      【解决方案3】:

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

                    <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>
      

      【讨论】:

        【解决方案4】:

        你可以使用线性布局来实现,复制粘贴这段代码看看是否有效

        <?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>
        

        【讨论】:

          猜你喜欢
          • 2020-12-17
          • 1970-01-01
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-07
          • 1970-01-01
          相关资源
          最近更新 更多