【问题标题】:How to change the width and height of listviews item in android?如何更改android中listview项目的宽度和高度?
【发布时间】:2014-08-07 01:27:48
【问题描述】:

我正在尝试构建一个应用程序,其中有一个包含许多项目的列表视图,但我无法更改或设置单个项目的宽度和高度。我到处搜索,我得到的答案是width fill_parent,但它不适合我...

请帮助....提前致谢...这里是代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dp"
    tools:context=".CustomListViewAndroidExample" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" 
        android:layout_weight="1"/> 

</RelativeLayout>

【问题讨论】:

  • 您的列表适配器视图(您想要添加到列表视图的视图)在哪里?也发布 xml 代码和您的适配器类

标签: android android-intent android-activity


【解决方案1】:

如果要动态改变列表视图的高度,可以使用

list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,    theSizeIWant)); 

import android.view.ViewGroup.LayoutParams;
ListView mListView = (ListView)   findViewById(R.id.listviewid);
LayoutParams list = (LayoutParams) mListView.getLayoutParams();
list.height = set the height acc to you;//like int  200
mListView.setLayoutParams(list);

【讨论】:

    【解决方案2】:

    This link 向您展示了如何使用您自己的自定义适配器在 java 中进行操作。

    在您的适配器中覆盖 getView() 时,您可以在将视图提供给框架进行渲染之前修改高度。另外请注意,您不必使用 SimpleCursorAdapter,也可以以相同的方式使用 ArrayAdapter。

    final SimpleCursorAdapter adapter = new SimpleCursorAdapter (context, cursor) {
        @Override
        public View getView (int position, View convertView, ViewGroup parent) {
            final View view = super.getView(position, convertView, parent);
            final TextView text = (TextView) view.findViewById(R.id.tvRow);
            final LayoutParams params = text.getLayoutParams();
    
            if (params != null) {
                    params.height = mRowHeight;
            }
    
            return view;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-25
      • 1970-01-01
      • 2011-03-13
      • 2014-01-12
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多