0

我有一个GridView包含类似瓷砖的按钮,其中这个数字可以在一定的范围内变化。我希望它GridView以给定的高度(例如,50%)出现在屏幕底部。使用垂直LinearLayout和权重,我可以使GridView50% 高,但其中的按钮更大,使得 v 变成可滚动区域。

我怎样才能避免这种情况?我希望其中的按钮能够精确缩放,以便GridView包含所有按钮而不滚动,并且占据屏幕的 50%。GridView甚至是错误的选择吗?

我现在不会发布代码,因为这不是一个特定的问题,而是更多关于如何解决这个问题的一般问题。如果需要,我当然会发布我到目前为止所得到的。

编辑:我现在尝试使用 a TableLayout,但单元格不会拉伸到屏幕宽度。其他线程说这是使用 (correct) 修复的LayoutParams,但它对我不起作用。我的 XML 文件是

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >

    <TableRow android:id="@+id/dummy"
        android:layout_weight="1" ></TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_weight="1" >
        <TableLayout android:id="@+id/example_table"
            android:layout_width="match_parent"
            android:layout_height="match_parent" ></TableLayout>        
    </TableRow>

</TableLayout>

Fragment正在使用它

@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState ) {
    return inflater.inflate( R.layout.fragment_example, container, false );
}

@Override
public void onActivityCreated( Bundle savedInstanceState ) {
    super.onActivityCreated( savedInstanceState );

    TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams(
        TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT );
    TableRow.LayoutParams cellParams = new TableRow.LayoutParams(
        TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT );

    TableLayout table = (TableLayout) getView().findViewById( R.id.example_table );
    for( int rowIndex = 0; rowIndex < 10; rowIndex++ ) {
        TableRow currentRow = new TableRow( getActivity() );

        for( int columnIndex = 0; columnIndex < 2; columnIndex++ ) {
            TextView cell = new TextView( getActivity() );
            cell.setBackgroundColor( Color.GREEN );
            cell.setText( "Foo" );

            currentRow.addView( cell, cellParams );
        }

        table.addView( currentRow, rowParams );
    }
}

它显示表格,但单元格尽可能窄,即它们几乎不环绕单词。不过,我希望单元格可以延伸到整个屏幕宽度。该表格也位于页面中间,而不是在我期望的底部(?)

编辑#2:好吧,没关系,我想通了。我必须设置weight基本上无处不在,然后它的工作原理。

4

0 回答 0