Android 以编程方式在TableRow内对齐TextView
我正在以编程方式创建TextView和TableRows,并尝试将其中一个TextView(名为Status:1)与TableRow内的右侧对齐 当前结果:Android 以编程方式在TableRow内对齐TextView,android,Android,我正在以编程方式创建TextView和TableRows,并尝试将其中一个TextView(名为Status:1)与TableRow内的右侧对齐 当前结果: 预期结果: 代码: TableRow tr = new TableRow(getContext()); tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT)
预期结果:
代码:
TableRow tr = new TableRow(getContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT));
ImageView profileImage = new ImageView(getContext());
profileImage.setLayoutParams(new TableRow.LayoutParams(128, 128));
TextView usernameText = new TextView(getContext());
TextView friendStatus = new TextView(getContext());
tr.setGravity(Gravity.CENTER_VERTICAL);
friendStatus.setGravity(Gravity.RIGHT); // It's not pushing the textview to the right?
usernameText.setText(username);
usernameText.setTextColor(Color.parseColor("#000000"));
friendStatus.setText("Status: " + status);
tr.addView(profileImage);
tr.addView(usernameText);
tr.addView(friendStatus);
tableLayout.addView(tr);
引用为TableLayout的TableLayout是用XML硬编码的
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
>
<TableLayout
android:id="@+id/tableLayout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
通过android:stretchColumns=“2”
谢谢,它现在可以正常工作了。如果其他任何人遇到这个问题,这个解决方案是可行的,但您仍然需要通过代码设置重力。设置重力(重力,右);就我而言。当然,我同意我们必须调用setGravity
或setxtalignment
<TableLayout
android:id="@+id/tableLayout"
android:orientation="horizontal"
android:stretchColumns="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>