我有一对垂直排列的 GridView,它们由适配器填充按钮。我可以让 GridViews 填充父视图的宽度,但我无法让第二个 GridView 填充父视图的高度。
GridViews 的高度似乎是固定的,无法更改。如果 GridView 有足够的空间,它们不会填充父级。如果没有足够的空间,它们会进入滚动模式——并且滚动模式不能被关闭。
我不确定,但 GridView 似乎对子按钮视图使用固定高度,我无法弄清楚按钮(行)高度是如何确定的,或者我如何更改它或强制 GridView 填充父级高度。
这是 GridViews 的 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 的高度关系。这也不会填充父级,如果其中一个视图的权重太小,它就会进入滚动模式。“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 数组。它解决了另一个问题。)
即使在视图被回收时,按钮的高度(和宽度)也为零,设置它没有任何效果。
GridView 或按钮的高度或填充属性是否可以在填充 GridView 之前在 Layout 参数中设置?