android - GridView 的高度是如何确定的,是否可以更改?

标签 android gridview

我有一对垂直排列的 GridView,它们由适配器填充按钮。我可以让 GridView 填充父 View 的宽度,但我无法让第二个 GridView 填充父 View 的高度。

看起来 GridViews 的高度是固定的,不能改变。如果 GridView 有足够的空间,它们就不会填满父 View 。如果没有足够的空间,它们就会进入滚动模式——而且滚动模式无法关闭。

我不确定,但 GridView 似乎为子按钮 View 使用固定高度,我无法弄清楚按钮(行)高度是如何确定的,或者我如何更改它或强制 GridView填充父级高度。

这是 GridView 的 XML:

            <LinearLayout
                android:id="@+id/keypads"
                android:background="@color/keypad_background_color"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:isScrollContainer="false"
                android:orientation="vertical" >
            <GridView
                android:id="@+id/grdMeasButtons"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:numColumns="4"
                android:scrollbars="none"
                android:isScrollContainer="false"
                android:stretchMode="columnWidth" />

            <GridView
                android:id="@+id/grdNumButtons"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="3"
                android:isScrollContainer="false"
                android:scrollbars="none"
                android:stretchMode="columnWidth" />
            </LinearLayout>

请注意,我的原始版本为每个 GridView 设置了 0px 的高度和 1:2 的权重比例,以设置 GridView 的高度关系。这也不会填充父 View ,如果其中一个 View 的权重太小,它就会进入滚动模式。 “isScrollContainer”属性不会关闭它。

这是来自适配器的 getView() 方法:

    // create a new ButtonView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    Button btn;
    if ((convertView == null) || !initializing) { // if it's not recycled, initialize some
                                // attributes

        btn = new Button(mContext);
        KeypadButton keypadButton = mButtons[position];

        switch(keypadButton.mCategory)
        {
        case NUMBER:
            btn.setBackgroundResource(R.drawable.keypadnumber);
            break;
        case DECIMAL:
            btn.setBackgroundResource(R.drawable.keypadnumber);
            break;
        case CLEAR:
            btn.setBackgroundResource(R.drawable.keypadnumber);
            break;
        case WEIGHT:    
        case VOLUME:
            switch (unitState[position]) {
            case SELECTED:
                btn.setEnabled(true);
                btn.setBackgroundResource(R.drawable.keypad_measure_selected);
                btn.setTextColor(mContext.getResources().getColor(R.color.units_text_enabled));
                break;
            case ENABLED:
                btn.setEnabled(true);
                btn.setBackgroundResource(R.drawable.keypad_measure_not_selected);
                btn.setTextColor(mContext.getResources().getColor(R.color.units_text_enabled));
                break;
            case DISABLED:
                btn.setEnabled(false);
                btn.setBackgroundResource(R.drawable.keypad_measure_not_selected);
                btn.setTextColor(mContext.getResources().getColor(R.color.units_text_disabled));
                break;
            default:
                break;
            }
            break;
        case DUMMY:
            break;
        default:
            btn.setBackgroundResource(R.drawable.keypadnumber);
            break;
        }
        // Set OnClickListener of the button to mOnButtonClick
        if(keypadButton != KeypadButton.DUMMY)
            btn.setOnClickListener(mOnButtonClick);
        else
            btn.setClickable(false);
        // Set CalculatorButton enumeration as tag of the button so that we
        // will use this information from our main view to identify what to do
        btn.setTag(keypadButton);
    } else {
        btn = (Button) convertView;
    }

    btn.setText(mButtons[position].getText());
    return btn;
}

(不用担心 unitState 数组。它解决了另一个问题。)

即使在回收 View 时,按钮的高度(和宽度)为零,设置它也没有效果。

是否可以在填充 GridView 之前在 Layout 参数中设置 GridView 或按钮的高度或填充属性?

最佳答案

// Just replace your second GridView With This One
<GridView
        android:id="@+id/grdNumButtons"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:isScrollContainer="false"
        android:numColumns="3"
        android:scrollbars="none"
        android:stretchMode="columnWidth" />

关于android - GridView 的高度是如何确定的,是否可以更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18712557/

相关文章:

android - ADB Bluestacks - 连接 2 台 PC

android - com.android.ide.common.process.ProcessException : Failed to execute aapt

java - 是否可以在Alertdialog中创建GridView?

Android 无法将新创建的位图流写入 WCF 服务

android - 如何使用带有 fragment 的 Firebase 电子邮件登录?

使用 H2 的 Android 示例

android - android中的Gridview布局

android - 使用 Spinner 处理 GridView 单元格中的点击事件

asp.net - 具有未知高度的 ASP.NET Gridview 的 DIV 背景

php - 如何在 Yii 中渲染空网格表?