Android:wrap_content不能与ListView一起使用



我正在研究安卓。我希望我的列表视图水平包装其内容,而不是填充所有宽度。wrap_content属性不起作用。怎么办?

为了在ListView中达到wrap_content高度,我们需要使用extends我们的本机ListViewCustomListView

我的列表视图.java

public class MyListView extends ListView {
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyListView(Context context) {
        super(context);
    }
    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

在您的layout使用这样的自定义视图,

布局.xml

<com.yourpackagename.MyListView
        ...       
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ... />

正如Romain Guy(Google工程师在UI工具包上工作)在他的帖子中所说

通过将宽度设置为 wrap_content您告诉 ListView 与其子项中最宽的宽度一样宽。因此,ListView 必须测量其项,并获取必须在适配器上调用 getView() 的项。这可能会发生多次,具体取决于布局通道的数量、父布局的行为等。

因此,如果您将 ListView 的布局宽度或布局高度设置为wrap_content则 ListView 将尝试测量附加到它的每个视图 - 这绝对不是您想要的。

请记住:始终避免为 ListView 或 GridView 设置wrap_content,有关更多详细信息,请参阅此 Google I/O 视频,讨论列表视图的世界

它可以通过使用LinearLayout来实现。

创建如下示例:

public class LinearLayoutAdapter   {
    LinearLayout linearLayout;
    ArrayList<String>  arrayList 
    private View rootView;
    private static LayoutInflater inflater;
    public ChoicesAdapter_(ArrayList<String>  arrayList ,LinearLayout linearLayout)
    {
        this.index =index;
        this.arrayList=arrayList;
        this.linearLayout=linearLayout;
        notifyDataSetChanged();
    }
    private void notifyDataSetChanged() {
        linearLayout.removeAllViews();
        for (int i =0;i<getCount();i++){
            linearLayout.addView(getView(i,null,null));
        }
    }
    public int getCount() {
        return  arrayList.size();
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
       rootView = inflater.inflate(R.layout.row_list, null);
       TextView  textView = (TextView)rootView.findViewById(R.id.text);    
       textView.setText(arrayList.get(position));
        return rootView;
    }
}

主活动示例:

public class MainActivity extends Activity    {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String>  arrayList = new  ArrayList<String>();
        arrayList.add("Accent");
        arrayList.add("Acclaim");
        arrayList.add("Accord");
        arrayList.add("Achieva");
        arrayList.add("Aerio");
        arrayList.add("Aerostar");
        LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
        linearLayoutAdapter= LinearLayoutAdapter(arrayList,linearLayout);
    }
}

XML 示例:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:textColor="@color/dark_gray"
    android:background="@color/white"
    android:textSize="20dp"
    android:text=""  />
I want my list view to wrap its content horizontally and not to fill all the width. 

在列表视图的情况下,你不能给出wrap_content,因为如果你为列表视图提供wrap_content,只有前三个项目(行)将显示其余的将被忽略。所以不要使用wrap_content..始终对列表视图使用 fill_parent 或 match_parent

为了在列表视图上进行高效设计,您可以看到列表视图的世界

我知道

我回答这个问题有点太晚了。但在我看来,如果您希望 ListView 具有某种wrap_content行为,则可以将其开始和结束边距设置为某个静态值。这在我的情况下有效。如果您担心针对各种屏幕尺寸执行此操作,则有一个非常有用的库来处理这种情况。可扩展的 DP。请在github中检查一次。

最新更新