将膨胀布局的子项添加到 TableRow 时相互重叠
Inflated layout's children overlapping with each other when adding them to a TableRow
我正在尝试扩充包含一些 textView 的布局。当我将其添加到 tableRow 时,该布局内的 textViews 会重叠。换句话说,我猜 layout_width 和 layout_height 被忽略了。
但是当我在充气后添加rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
时,什么也没有出现。
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.layout_result_row, null);
rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
layout_result_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/results_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp">
<TextView
android:id="@+id/results_row_match_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="@+id/results_row_turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/results_row_match_no"
android:layout_alignParentEnd="true"
android:text="TextView" />
<TextView
android:id="@+id/results_row_teams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/results_row_match_no"
android:text="TextView" />
<TextView
android:id="@+id/results_row_turn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/results_row_turn"
android:layout_toStartOf="@+id/results_row_turn"
android:text="TextView" />
<TextView
android:id="@+id/results_row_toss_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/results_row_match_no"
android:layout_marginTop="22dp"
android:text="TextView" />
<TextView
android:id="@+id/results_row_home_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/results_row_toss_ml"
android:layout_alignBottom="@+id/results_row_toss_ml"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="@+id/results_row_rank_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/results_row_home_ml"
android:layout_toEndOf="@+id/results_row_turn_text"
android:text="TextView" />
<TextView
android:id="@+id/results_row_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/results_row_turn_text"
android:layout_alignStart="@+id/results_row_home_ml"
android:text="TextView" />
</RelativeLayout>
而不是:
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
尝试:
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
膨胀的布局已经有了布局参数,所有的 chid 视图都不会用新的布局参数重绘。如果你想在膨胀后拥有与以前相同的视图,你可以使用下面的代码。
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setBackgroundColor(Color.RED);
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.activity_main, null);
tableLayout.setStretchAllColumns(true);
// rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
setContentView(tableLayout);
我正在尝试扩充包含一些 textView 的布局。当我将其添加到 tableRow 时,该布局内的 textViews 会重叠。换句话说,我猜 layout_width 和 layout_height 被忽略了。
但是当我在充气后添加rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
时,什么也没有出现。
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.layout_result_row, null);
rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
layout_result_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/results_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp">
<TextView
android:id="@+id/results_row_match_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="@+id/results_row_turn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/results_row_match_no"
android:layout_alignParentEnd="true"
android:text="TextView" />
<TextView
android:id="@+id/results_row_teams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/results_row_match_no"
android:text="TextView" />
<TextView
android:id="@+id/results_row_turn_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/results_row_turn"
android:layout_toStartOf="@+id/results_row_turn"
android:text="TextView" />
<TextView
android:id="@+id/results_row_toss_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/results_row_match_no"
android:layout_marginTop="22dp"
android:text="TextView" />
<TextView
android:id="@+id/results_row_home_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/results_row_toss_ml"
android:layout_alignBottom="@+id/results_row_toss_ml"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="@+id/results_row_rank_ml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/results_row_home_ml"
android:layout_toEndOf="@+id/results_row_turn_text"
android:text="TextView" />
<TextView
android:id="@+id/results_row_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/results_row_turn_text"
android:layout_alignStart="@+id/results_row_home_ml"
android:text="TextView" />
</RelativeLayout>
而不是:
TableRow.LayoutParams lp = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
尝试:
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
膨胀的布局已经有了布局参数,所有的 chid 视图都不会用新的布局参数重绘。如果你想在膨胀后拥有与以前相同的视图,你可以使用下面的代码。
TableLayout tableLayout = new TableLayout(getApplicationContext());
tableLayout.setBackgroundColor(Color.RED);
TableRow tableRow = new TableRow(this);
TableRow.LayoutParams lp = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tableRow.setLayoutParams(lp);
View rowView = getLayoutInflater().inflate(R.layout.activity_main, null);
tableLayout.setStretchAllColumns(true);
// rowView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
// loadResultRow(rowView, result); //This method sets the text of the textViews
tableRow.addView(rowView);
tableLayout.addView(tableRow);
setContentView(tableLayout);