【问题标题】:Is it possible to set listView maxHeight in xml?是否可以在 xml 中设置 listView maxHeight?
【发布时间】:2015-07-14 05:06:55
【问题描述】:

拥有listView,如果内容较少,则其高度应与"wrap_content" 一致。如果它有更多行,则最大高度应限制在某个高度。

ListView中允许设置android:maxHeight

<ListView>
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxHeight="120dp"
</ListView>

但它不起作用并且总是"wrap_content"。 让它工作的唯一方法是在代码使用中

int cHeight = parentContainer.getHeight();
ViewGroup.LayoutParams lp = mListView.getLayoutParams();

if (messageListRow > n) 
{
    lp.height = (int)(cHeight * 0.333);
} 
else 
{
    lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}

mListView.setLayoutParams(lp);

有没有办法在xml中做到这一点?

【问题讨论】:

  • 不要使用“wrap_content”作为列表视图的高度,因为它会导致你的适配器被多次调用。你为什么不把你的listview放在一个LinearLayout容器中,然后根据你的需要控制高度。
  • 好点,谢谢 Pztar。但是即使放在一个 LinearLayout 容器中,定义容器的高度仍然是个问题。也许在代码中动态设置是唯一的方法。

标签: android android-listview height


【解决方案1】:

是的,您可以使用带有 maxHeight 属性的自定义 ListView

步骤 1. 在values 文件夹中创建attrs.xml 文件并输入以下代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="ListViewMaxHeight">
        <attr name="maxHeight" format="dimension" />
    </declare-styleable>

</resources>

第 2 步。创建一个新类 (ListViewMaxHeight.java) 并扩展 ListView 类:

package com.example.myapp;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ListView;

public class ListViewMaxHeight extends ListView {

    private final int maxHeight;

    public ListViewMaxHeight(Context context) {
        this(context, null);
    }

    public ListViewMaxHeight(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ListViewMaxHeight(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ListViewMaxHeight);
            maxHeight = a.getDimensionPixelSize(R.styleable.ListViewMaxHeight_maxHeight, Integer.MAX_VALUE);
            a.recycle();
        } else {
            maxHeight = 0;
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (maxHeight > 0 && maxHeight < measuredHeight) {
            int measureMode = MeasureSpec.getMode(heightMeasureSpec);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, measureMode);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}

第 3 步。在布局的 xml 文件中:

<com.example.myapp.ListViewMaxHeight
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:maxHeight="120dp" />

【讨论】:

  • 这不是一个好的解决方案。我在我的适配器中使用了它,我看到 position 的项目没有真正的价值。
  • 完美运行。谢谢
猜你喜欢
  • 2023-03-13
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
相关资源
最近更新 更多