我正在尝试在 4x4 网格视图中设计一个带有按钮的计算器。现在,当我生成视图时,网格下方有空白区域。我希望网格完全填充父级。就像http://rechner-app.com/示例一样。这个怎么做。
activity_main.xml
<TextView
android:id="@+id/txtStack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="3sp"
android:gravity="right"
android:textSize="15sp" />
<TextView
android:id="@+id/txtInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:gravity="right"
android:textSize="25sp" />
<TextView
android:id="@+id/txtMemory"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:gravity="left"
android:textSize="15sp" />
<LinearLayout
android:id="@+id/gridContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_horizontal"
android:orientation="vertical" >
<GridView
android:id="@+id/grdButtons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="0.5dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0.5dp" />
</LinearLayout>
适配器。
public View getView(int position, View convertView, ViewGroup parent) {
Button btn;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
btn = new Button(mContext);
KeypadButton keypadButton = mButtons[position];
btn.setBackgroundResource(R.drawable.keypad1);
// 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);
View rootView = LayoutInflater.from(mContext).inflate(R.layout.activity_main, parent, false);
GridView mKeypadGridContainer = (GridView) rootView.findViewById(R.id.grdButtons);
Log.d(TAG, mKeypadGridContainer.getHeight() + "sadsd");
Log.d(TAG, mKeypadGridContainer.getHeight() + " :height");
//btn.setMinimumHeight(MainActivity.metricsHeight/4);
} else {
btn = (Button) convertView;
}
btn.setText(mButtons[position].getText());
return btn;
}