Android:以编程方式创建的布局没有出现?

时间:2017-02-23 07:08:12

标签: android layout

我认为这个问题已被提出很多,但我没有能够通过所有类似的帖子找到答案。没有错误,但是当应用程序运行时,没有任何错误出现。所以这是我活动中的代码。

public class MainGameActivity extends AppCompatActivity {

    int width = 2;
    int height = 4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        RelativeLayout mainLayout = (RelativeLayout) View.inflate(this, R.layout.activity_main_game, null);
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));
        Table gameTable = new Table(width, height);
        ArrayList<TableRow> tableRowArray = new ArrayList<TableRow>();


        for (int i = 0; i < height; i++) {
            tableRowArray.add(new TableRow(this));
            tableRowArray.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                ViewGroup.LayoutParams.WRAP_CONTENT));
            for (int j = 0; j < width; j++) {
                Button button = new Button(this);
                button.setText(i + "." + j);
                button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                tableRowArray.get(i).addView(button);
            }
            tableLayout.addView(tableRowArray.get(i));
        }
        mainLayout.addView(tableLayout);
        setContentView(mainLayout);
    }

}

这是我的XML,基本上什么都没有:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main_game"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.khangbuin.flyingdiscscanner.MainGameActivity">

</RelativeLayout>

提前感谢您的回答。

0 个答案:

没有答案