代码之家  ›  专栏  ›  技术社区  ›  Chrobin

Android:TableLayout不显示

  •  0
  • Chrobin  · 技术社区  · 7 年前

    我是Android应用程序编程新手。

    我正在尝试使用表格布局列出一些注释。因为它们的数量不应该受到限制,所以我必须通过编程将其集成。

    我为测试目的创建了一个新项目:

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.FrameLayout;
    import android.widget.ScrollView;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    
    public class MainActivity extends AppCompatActivity {
    
        private TableLayout table;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            LayoutInflater inflater = (LayoutInflater) getSystemService(MainActivity.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.activity_main, null);
    
            table = view.findViewById(R.id.table);
            table.setOrientation(TableLayout.HORIZONTAL);
    
    
            TableRow tr = new TableRow(MainActivity.this);
            TableRow.LayoutParams paramrow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
            tr.setLayoutParams(paramrow);
    
            Button b = new Button(this);
            b.setText("Dynamic Button");
            b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
            tr.addView(b);
            table.addView(tr, TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
        }
    }
    

    我的XML:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.chrobin.beung.MainActivity">
    
        <TableLayout
            android:id="@+id/table"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableLayout>
    </android.support.constraint.ConstraintLayout>
    

    它不是重复的,因为现在所有的答案都涉及到使用TableRow。LayoutParams代替os LayoutParams。

    谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sachin Rajput KgaboL    7 年前

    使用以下代码更改XML文件 0dp height width 属于 your table 所以它不起作用 ,我已更正您的代码-

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.chrobin.beung.MainActivity">
    
        <TableLayout
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableLayout>
    </android.support.constraint.ConstraintLayout>