Android程序表填充单元格

时间:2018-03-07 21:22:36

标签: android android-layout android-button

我正在尝试以编程方式使用按钮填充TableLayout。但是我希望它们能够填满整个可用空间。

但是按钮并没有使用它们的全部高度,而且它们的边距被忽略了。请参阅this screenshot

如何强制它们填充所有可用空间,并在它们之间留有一些余量?结果应该基本上是一个按钮网格,它们之间有一些边距,但是拉伸整个屏幕。

继承我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TableLayout myTable = findViewById(R.id.myTable);

        TableLayout.LayoutParams tableRowLayoutParams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);

        int counter = 1;
        for (int i = 0; i < 6; i++) {
            TableRow tableRow = new TableRow(this);
            tableRow.setLayoutParams(tableRowLayoutParams);

            for (int j = 0; j < 5; j++) {
                Button b = new Button(this, null, R.attr.myButtonStyle);
                b.setText("Button " + counter);
                tableRow.addView(b, j);

                counter++;
            }

            myTable.addView(tableRow);
        }
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myTable"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    tools:context="com.example.tabletest.MainActivity">

</TableLayout>

通过attrs引用的按钮样式:

<style name="myButton">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_margin">5dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:layout_gravity">fill</item>
    <item name="android:gravity">center</item>
    <item name="android:padding">0dp</item>

    <item name="android:textColor">@android:color/primary_text_dark</item>
    <item name="android:background">@android:color/holo_blue_dark</item>
    <item name="android:textSize">45sp</item>
</style>

1 个答案:

答案 0 :(得分:0)

我也不能这样做。

但是,解决方法是将TableLayout替换为垂直LinearLayout,然后将TableRow替换为另一个LinearLayout,这次是水平的。然后将按钮设置为layout_width = 0layout_weight = 1。 (如有必要,还要添加保证金)

然后您应该拥有以下代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout myTable = findViewById(R.id.myTable);

        LinearLayout.LayoutParams tableRowLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f);

        LinearLayout.LayoutParams tableBtnParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f);
        int tableBtnMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
        tableBtnParams.setMargins(tableBtnMargin,tableBtnMargin,tableBtnMargin,tableBtnMargin);

        int counter = 1;
        for (int i = 0; i < 6; i++) {
            LinearLayout tableRow = new LinearLayout(this);
            tableRow.setLayoutParams(tableRowLayoutParams);

            for (int j = 0; j < 5; j++) {
                Button b = new Button(this, null, R.attr.myButtonStyle);
                b.setLayoutParams(tableBtnParams);
                b.setText("Button " + counter);
                tableRow.addView(b, j);

                counter++;
            }

            myTable.addView(tableRow);
        }
    }
}

Final Product