如何在android中以编程方式设置相等的列宽度大小

时间:2018-04-22 19:43:25

标签: java android android-layout android-fragments android-tablelayout

我有这个布局:

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

    <Spinner
        android:id="@+id/spinner_strassenfuehrer_fb_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
    android:id="@+id/header_strassenfuehrer_screen_fb"
    android:layout_span="8"
    android:background="#A6A6A6"
    android:stretchColumns="*"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">
</TableLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TableLayout
    android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
    android:layout_span="8"
    android:stretchColumns="*"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>

这部分代码:

 private TableLayout tableLayout;
    private TableLayout tableHeader;

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

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        //determine height and width
        int width = metrics.widthPixels;

        //read database and put values dynamically into table
        tableLayout = (TableLayout) findViewById(R.id.tablelayout_strassenfuehrer_fb_screen);

        tableHeader = (TableLayout) findViewById(R.id.header_strassenfuehrer_screen_fb);

        //initialize header row and define LayoutParams
        TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f);

        TableRow header_row = new TableRow(this);

        //column 1
        TextView header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Störcode");
        header_row.addView(header_tv);

        //column 2
        header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Bezeichnung");
        header_row.addView(header_tv);

        //column 3
        header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Letztes Auftreten");
        header_row.addView(header_tv);

        //column 4
        header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Anlagen ID");
        header_row.addView(header_tv);

        //column 5
        header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Projekt");
        header_row.addView(header_tv);

        //column 6
        header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Benötigte Ersatzteile");
        header_row.addView(header_tv);

        //column 7
        header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Benötigtes Werkzeug");
        header_row.addView(header_tv);

        //column 8
        header_tv = new TextView(this);
        header_tv.setLayoutParams(params);
        header_tv.setText("Handlungsleitfaden");
        header_row.addView(header_tv);

        tableHeader.addView(header_row, 0);

        for (int i = 0; i < 100; i++) {

            TableRow row = new TableRow(this);

            //column 1
            TextView tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST");
            row.addView(tv);

            //column 2
            tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST");
            row.addView(tv);

            //column 3
            tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST");
            row.addView(tv);

            //column 4
            tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST");
            row.addView(tv);

            //column 5
            tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST");
            row.addView(tv);

            //column 6
            tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST");
            row.addView(tv);

            //column 7
            tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST");
            row.addView(tv);

            //column 8
            tv = new TextView(this);
            tv.setLayoutParams(params);
            tv.setText("TEST " + i);
            row.addView(tv);

            tableLayout.addView(row, i);
        }
}

我试图确定并设置我的行的列相等。

但我得到的是: enter image description here

有人知道,如何正确确定宽度header_rowrows下的header_row会在同一行吗?

我是android的新手并且还阅读了一些文档,但还没找到解决方案。 :(

谢谢!

编辑: 我更新了代码,但是标题行和行看起来仍然不太好。 我想,所有的价值观都在彼此之间。 谢谢! :)

1 个答案:

答案 0 :(得分:1)

您需要在android:stretchColumns="*"声明中使用TableLayout来使列的宽度相等。因此,请按如下方式修改布局。

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

    <Spinner
        android:id="@+id/spinner_strassenfuehrer_fb_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
        android:id="@+id/header_strassenfuehrer_screen_fb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_span="8"
        android:background="#A6A6A6"
        android:stretchColumns="*"></TableLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TableLayout
            android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="8"
            android:stretchColumns="*" />
    </ScrollView>
</LinearLayout>

希望有所帮助!