在将视图的XML代码直接重新创建到java代码之后,它将停止正常工作。
XML代码是:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.virviil.splashscreen.MainActivity">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.7" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.3" />
</TableLayout>
java代码是:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout activity_main = new TableLayout(this);
activity_main.setId(R.id.activity_main);
LayoutParams layout_439 = new LayoutParams(0,0);
layout_439.width = LayoutParams.MATCH_PARENT;
layout_439.height = LayoutParams.MATCH_PARENT;
activity_main.setLayoutParams(layout_439);
TableRow tableRow_879 = new TableRow(this);
LayoutParams layout_311 = new LayoutParams(0,0);
layout_311.width = LayoutParams.MATCH_PARENT;
layout_311.height = LayoutParams.MATCH_PARENT;
layout_311.weight = 0.7f;
tableRow_879.setLayoutParams(layout_311);
activity_main.addView(tableRow_879);
TableRow tableRow_600 = new TableRow(this);
tableRow_600.setGravity(Gravity.CENTER_HORIZONTAL);
LayoutParams layout_61 = new LayoutParams(0,0);
layout_61.width = LayoutParams.MATCH_PARENT;
layout_61.height = LayoutParams.MATCH_PARENT;
tableRow_600.setLayoutParams(layout_61);
ProgressBar progressBar_343 = new ProgressBar(this);
LayoutParams layout_81 = new LayoutParams(0,0);
layout_81.width = LayoutParams.WRAP_CONTENT;
layout_81.height = LayoutParams.WRAP_CONTENT;
progressBar_343.setLayoutParams(layout_81);
tableRow_600.addView(progressBar_343);
activity_main.addView(tableRow_600);
TableRow tableRow_315 = new TableRow(this);
LayoutParams layout_574 = new LayoutParams(0,0);
layout_574.width = LayoutParams.MATCH_PARENT;
layout_574.height = LayoutParams.MATCH_PARENT;
layout_574.weight = 0.3f;
tableRow_315.setLayoutParams(layout_574);
activity_main.addView(tableRow_315);
setContentView(activity_main);
//setContentView(R.layout.activity_main);
}
答案 0 :(得分:0)
您唯一需要注意的是在整个xml视图中使用LinearLayout而不是TableLayout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.virviil.splashscreen.MainActivity">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
....