【问题标题】:ListView applies wrap_content to its items on androidListView 将 wrap_content 应用于其在 android 上的项目
【发布时间】:2017-09-23 08:33:28
【问题描述】:

我有ListView,我想展示具有固定高度的自定义项目。项目布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="horizontal">

    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyle"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        app:srcCompat="@drawable/login_btn_facebook" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:text="TextView"
        android:textColor="@color/gameMenuItem"
        android:textSize="16sp"/>

</LinearLayout>

注意根LinearLayout 的固定高度。 适配器类:

public class SelectorMenuLoggedOutAdapter extends BaseAdapter {
    public static final int ACTION_LOGIN_FACEBOOK=0;
    public static final int ACTION_LOGIN_GOOGLE=1;
    public static final int ACTION_FEEDBACK=2;
    public static final int ACTION_RATE_US=3;
    private LayoutInflater inflater;
    private Context context;
    public SelectorMenuLoggedOutAdapter(Context ctx) {
        inflater=LayoutInflater.from(ctx);
        context=ctx;
    }
    @Override
    public int getCount() {
        return 2;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View res=convertView;
        if (res==null) {
            switch (position) {
                case 0:
                    res = inflater.inflate(R.layout.selector_menu_item_login,null);
                    ((TextView)res.findViewById(R.id.title)).setText(context.getText(R.string.selector_menu_login_facebook));
                    ((ImageView)res.findViewById(R.id.icon)).setImageResource(R.drawable.login_btn_facebook);
                    res.findViewById(R.id.progress).setVisibility(View.GONE);
                    break;
                case 1:
                    res = inflater.inflate(R.layout.selector_menu_item_login,null);
                    ((TextView)res.findViewById(R.id.title)).setText(context.getText(R.string.selector_menu_login_google));
                    ((ImageView)res.findViewById(R.id.icon)).setImageResource(R.drawable.login_btn_google);
                    res.findViewById(R.id.progress).setVisibility(View.GONE);
                    break;
            }
        }
        return res;
    }
}

但是,当我将此适配器设置为我的列表视图时,它会像它们的 layout_height=wrap_content 一样显示行:

一些无用的细节告诉解析器它主要不是代码,没有什么可以添加的。

【问题讨论】:

  • 你到底想要怎么样,能不能在这里展示一下!以便我们更清楚地了解您的问题。你的问题有点不清楚。
  • 我希望列表项的高度与布局中所述的一样,而不是 wrap_content
  • 如果我没记错的话,你可以试试这个。将高度删除为 wrap_content 并将其设置为 match_parent 在您的视图中,即必须在列表视图中显示的视图。不要仅在 xml 代码中的视图中对 LinearLayout 进行任何更改。做它并判断它是否工作正常
  • 我的布局中没有 wrap_content(如果你检查它,你会看到它的高度是 200dp)。我希望我的项目高度为 40dp,但无论我为我的项目设置什么高度看起来都一样(fixed size/match_parent/wrap_content) listview 只是忽略它。

标签: android listview


【解决方案1】:

在您的 getView 方法中,您应该扩展您的视图,将父 ViewGroup 作为根并通过 false 作为 attachToRoot 参数。

inflater.inflate(R.layout.selector_menu_item_login, parent, false)

这将允许您的项目层次结构的父级获取一组 LayoutParams 值以按预期布局自身。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多