我想用行填充特定的TableLayout。但是,当我以编程方式执行此操作时,它看起来与我希望的有所不同。下面的示例与列的大小或距离有关,而与背景无关。我找不到错误。
希望你能帮助我。
XML :(它的外观):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/testLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50sp"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:width="0dp"
android:background="@drawable/test_drawablew"
android:text="Text 1"
android:textAlignment="center" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="0.2"
android:background="@android:color/darker_gray" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:width="0dp"
android:background="@drawable/test_drawablew"
android:text="Text 1.1"
android:textAlignment="center" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="0.2"
android:background="@android:color/darker_gray" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:width="0dp"
android:background="@drawable/test_drawablew"
android:text="Text 1.2"
android:textAlignment="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50sp"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="@drawable/test_drawablew"
android:text="Longer Text"
android:textAlignment="center" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="0.2"
android:background="@android:color/darker_gray" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="@drawable/test_drawablew"
android:text="Longer Text"
android:textAlignment="center" />
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="0.2"
android:background="@android:color/darker_gray" />
<TextView
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="@drawable/test_drawablew"
android:text="Longer Text"
android:textAlignment="center" />
</TableRow>
</TableLayout>
以编程方式:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);
TableLayout tableLayout = findViewById(R.id.permissionTableLayout);
ArrayList<FlowInformation> flowInformations = getIntent().getParcelableArrayListExtra("flowInformation");
TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
tableLayoutParams.height = TableLayout.LayoutParams.WRAP_CONTENT;
tableLayoutParams.weight = TableLayout.LayoutParams.MATCH_PARENT;
tableLayoutParams.bottomMargin = (int) getResources().getDimension(R.dimen.marginBottom);
for (FlowInformation flowInformation : flowInformations) {
TableRow tableRow = new TableRow(this);
tableRow.setGravity(Gravity.CENTER);
tableRow.setLayoutParams(tableLayoutParams);
tableRow.setId(View.generateViewId());
addNewTextViewToTableRow(tableRow, flowInformation.source);
TableRow.LayoutParams viewParams = new TableRow.LayoutParams();
viewParams.weight = (float) 0.2;
viewParams.height = (int) getResources().getDimension(R.dimen.fiveDp);
viewParams.width = TableRow.LayoutParams.MATCH_PARENT;
View view = new View(this);
view.setBackgroundColor(Color.RED);
view.setLayoutParams(viewParams);
view.setId(View.generateViewId());
tableRow.addView(view);
addNewTextViewToTableRow(tableRow, flowInformation.channel);
viewParams = new TableRow.LayoutParams();
viewParams.weight = (float) 0.2;
viewParams.height = (int) getResources().getDimension(R.dimen.fiveDp);
viewParams.width = TableRow.LayoutParams.MATCH_PARENT;
view = new View(this);
view.setBackgroundColor(Color.RED);
view.setLayoutParams(viewParams);
view.setId(View.generateViewId());
tableRow.addView(view);
addNewTextViewToTableRow(tableRow, flowInformation.target);
tableLayout.addView(tableRow);
}
}
public void addNewTextViewToTableRow(final TableRow tableRow, final String text) {
GradientDrawable background = new GradientDrawable();
background.setColor(Color.rgb(235, 235, 235));
background.setStroke(1, Color.rgb(0, 0, 0));
background.setShape(GradientDrawable.RECTANGLE);
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
tableRowParams.weight = (float) 0.2;
tableRowParams.height = TableLayout.LayoutParams.WRAP_CONTENT;
tableRowParams.width = (int) getResources().getDimension(R.dimen.nullDp);
TextView textView = new TextView(this);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setBackground(background);
textView.setText(text);
textView.setLayoutParams(tableRowParams);
textView.setId(View.generateViewId());
tableRow.addView(textView);
}