我有一个 ViewGroup,它有几个孩子。其中之一是 TextView(“+id/text”)。
在我的代码中,我想知道如何添加一个新的 View 或 ViewGroup,它将垂直对齐并位于 TextView (+“id/text”)下方?
谢谢。
我已遵循以下建议并尝试使用 TableLayout。
作为测试,我尝试静态布局以确保事物正确对齐。
这是我的布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/panel" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:layout_column="0" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_column="1">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left" />
</LinearLayout>
</TableRow>
<TableRow>
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/icon"
android:layout_column="1"/>
</TableRow>
</TableLayout>
但是当我在模拟器上运行时。第二行的 ImageButton 与第一行的 textView 不垂直对齐。知道为什么吗?
考虑使用 TableLayout 作为您的 ViewGroup。使用 TableLayout,您可以以编程方式在特定位置添加子项。
View view = new ViewIWantToAdd();
TableLayout layout = (TableLayout)findViewById(R.id.table_layout);
layout.addView(view);
将在 TableLayout 的底部添加一个视图。通常,您会使用 TableRow 作为 TableView 的内容,但您可以添加任何视图。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)