【问题标题】:How to right-align elements in table layout in Java?如何在 Java 中的表格布局中右对齐元素?
【发布时间】:2013-07-31 15:44:34
【问题描述】:

请查看以下 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"
    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)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);
}

但是,我得到的是以下内容

如您所见,动态生成的元素是左对齐的。我怎样才能使这些正确对齐?

【问题讨论】:

    标签: java android eclipse android-tablelayout android-scrollview


    【解决方案1】:

    解决方案 1:

    使用RelativeLayout,利用它的属性:

    android:layout_alignParentLeft="true" 替换子代TextViewandroid:layout_alignParentRight="true" 替换CheckBox.

    相对于父母边界定位孩子。您可能还想添加android:layout_centerVertical="true" to each child to center the two vertical within each list item.

    解决方案 2:

    如果你想把它放在桌子上, 在 Java 中使用 myTable.setColumnStretchable(2, true);。 和android:stretchColumns="2" in XML

    解决方案 3:

    使用CheckedTextView。我认为这将满足您的需求。

    希望对您有所帮助。

    【讨论】:

    • 感谢您的回复。但我需要右对齐我动态生成的内容,而不是我在 XML 中创建的文本视图
    • 你看到解决方案2了吗?
    • 是的,那也做得不好
    • 您可以尝试使用 LinearLayout 代替 TableLayout 吗?看看是否符合你的要求。它对我有用。表格布局有什么具体原因吗?
    • 我必须使用 ScrollView 并且我需要在行中添加超过 1 个元素。那么线性布局就不行了?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2012-08-27
    • 1970-01-01
    相关资源
    最近更新 更多