【问题标题】:Set ListView row height in code在代码中设置 ListView 行高
【发布时间】:2012-11-22 13:23:20
【问题描述】:

我有一个Activity,其中包含一个基于SimpleCursorAdapterListView 和一个按钮。 当我单击按钮时,我希望行高变大。 我决定在按钮OnClick 上使用TextView 高度:

TextView tvRow = (TextView) findViewById(R.id.tvRow);
tvRow.setHeight(20);

但它只更改列表的第一行。

我可以在AdaptergetView方法中改变高度:

TextView tvRow = (TextView) view.findViewById(R.id.tvRow);
tvRow.setHeight(20);

但这不是我需要的功能;我需要这个按钮OnClick

如何通过点击按钮更改ListView 行高? 甚至可能是一个技巧:) 感谢您的宝贵时间。

更新:

当我将此代码放入 SimpleCursorAdapter 时,一切正常,但在 BaseAdapter 中的方法 getView 中:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {
   View view = convertView;
if (view == null) {
  view = lInflater.inflate(R.layout.itemF, parent, false);
}
...   
 final LayoutParams params = view.getLayoutParams();
            if (params == null) { 
                view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight)); 
        }else { 
        params.height = mRowHeight; }
return view;

显示的列表忽略了这一点,它的行宽保持不变。怎么了?

更新:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<CheckBox
    android:id="@+id/cbBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<LinearLayout
    android:id="@+id/llRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Наименование фирмы"
        android:textAllCaps="true"
        android:textSize="14dp" />

    <TextView
        android:id="@+id/tvAddr"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Адрес фирмы"
        android:textSize="10dp" />
</LinearLayout>

【问题讨论】:

  • 所以您希望在单击按钮时所有行都变大?
  • 你的第二段代码和你的第一段是一样的,这是错字吗?
  • 是的,我希望所有行的大小相同——更大或更小。

标签: android listview button height


【解决方案1】:

我认为您必须更改SimpleCursorAdaptergetView() 方法,如下所示:

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

当您单击按钮时,您必须更改 mRowHeight 并通知 ListView 有关 adapter.notifyDataSetChanged() 之类的更改。对于mRowHeight 的第一个值,您可以设置:

mRowHeight = LayoutParams.WRAP_CONTENT

UPD:

如果您的BaseAdapter 的方法hasStableIds() 返回false(默认返回false),您必须在getView() 中应用少量更改(您必须手动将LayoutParams 设置为View) :

LayoutParams params = view.getLayoutParams();
if (params == null) { 
    params = new LayoutParams(LayoutParams.MATCH_PARENT, mRowHeight); 
} else {
    params.height = mRowHeight;
}

view.setLayoutParams(params);

【讨论】:

  • @Foenix,这取决于哪个布局是视图的父级。但是 ViewGroup.LayoutParams 在所有情况下都可能是合适的。
  • 我建议您更改最终视图视图的高度 = super.getView(position, convertView, parent)。但是在第一次初始化时,他的 LayoutParams 可以为空,这就是为什么你可以这样做: if (params == null) { view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT), mRowHeight) } else { params.height = mRowHeight; }
  • 这样更好,因为行可以包含其他几个视图,再次感谢您!
  • @Foenix,您需要更改宽度或高度(更新后的问题是关于宽度)?能否添加 itemF.xml 和方法 getView() 的完整代码。
  • @Foenix,我认为问题出在设置的 LayoutParams 中。我更新答案。
【解决方案2】:

我做了类似的事情:

@Override
public View getView(int position, View convertView,ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        TextView textView=(TextView) view.findViewById(android.R.id.text1);
        textView.setHeight(30);
        textView.setMinimumHeight(30);



        / * Couleur de votre choix * / 
        textView . SetTextColor ( Couleur . BLACK );



        retourner voir ; 
    }

您必须将两个字段都放入 textView.setHeight (30); textView.setMinimumHeight (30);否则它将无法正常工作。对我来说它有效,我遇到了同样的问题。

【讨论】:

    猜你喜欢
    • 2014-11-10
    • 1970-01-01
    • 2012-10-14
    • 2012-05-09
    • 2010-11-17
    • 2012-07-13
    • 1970-01-01
    • 2014-06-20
    • 2012-11-14
    相关资源
    最近更新 更多