如何在Java中对表布局中的元素进行右对齐?
请查看以下XML文件如何在Java中对表布局中的元素进行右对齐?,java,android,eclipse,android-tablelayout,android-scrollview,Java,Android,Eclipse,Android Tablelayout,Android Scrollview,请查看以下XML文件 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:stretchColumns="*"
android:padding="5dp" >
<TableRow
android:id="@+id/tableRow0"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Words To Remove" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp"
>
</ScrollView>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/removeWordsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Acerto"
/>
</TableRow>
</TableLayout>
在这个scrollview中,我添加了带有文本视图和按钮的表行,如下所示
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TableLayout internalTableLayout = new TableLayout(this);
for(int i=0;i<words.size();i++)
{
TableRow r = new TableRow(this);
//r.setId(i);
TextView t = new TextView(this);
t.setGravity(Gravity.RIGHT);
CheckBox c = new CheckBox(this);
r.addView(t);
r.addView(c);
internalTableLayout.addView(r);
}
scrollView.addView(internalTableLayout);
}
ScrollView ScrollView=(ScrollView)findViewById(R.id.ScrollView);
TableLayout internalTableLayout=新的TableLayout(本);
对于(int i=0;隔离1:
使用RelativeLayout,利用其属性:
对于TextView
用android:layout\u alignParentLeft=“true”
替换子项,对于复选框用android:layout\u alignParentRight=“true”
替换子项。
将子项相对于父项边框进行定位。您可能还希望将android:layout\u centerVertical=“true”添加到每个子项,以使每个列表项中的两个垂直项居中。
解决方案2:
如果你想把它放在桌子上,
在Java中使用myTable.setColumnStretchable(2,true);
。
以及XML中的android:stretchColumns=“2”
解决方案3:
我想这能满足你的需要
希望能有所帮助。谢谢你的回复。但我需要右对齐我动态生成的内容,而不是我在XML中创建的文本视图。你看到解决方案2了吗?是的,这也不太好。你可以尝试使用LinearLayout而不是TableLayout吗?看看它是否满足你的要求。它对我有效。表L的具体原因是什么布局?我必须使用ScrollView,并且我需要在行中添加多个元素。所以线性布局不能正常工作?