Android 两列表格布局,每列精确50%
自从我开始使用安卓系统以来,这个问题一直困扰着我。我不能在一个2列的表格布局中使两列各精确50% 下面是一个例子:Android 两列表格布局,每列精确50%,android,layout,resources,Android,Layout,Resources,自从我开始使用安卓系统以来,这个问题一直困扰着我。我不能在一个2列的表格布局中使两列各精确50% 下面是一个例子: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="fill_parent" > <TableLayout
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<TableLayout
android:id="@+id/tablelayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:paddingRight="2dip" >
<TableRow>
<TextView
style="@style/TextViewStandard"
android_layout_span="2"
android:layout_weight="1"
android:text="Bla" />
</TableRow>
<TableRow>
<TextView
style="@style/TextViewStandard"
android:layout_weight="1"
android:text="Name:" />
<EditText
style="@style/EditTextStandard"
android:id="@+id/et_name"
android:layout_weight="1" />
</TableRow>
<TableRow>
<TextView
style="@style/TextViewStandard"
android:layout_weight="1"
android:text="URL:" />
<EditText
style="@style/EditTextStandard"
android:id="@+id/et_url"
android:layout_weight="1" />
</TableRow>
<TableRow>
<Button
style="@style/ButtonStandard"
android:layout_column="0"
android:layout_marginTop="6dip"
android:onClick="onClickOk"
android:text="@android:string/ok" />
</TableRow>
</TableLayout>
</ScrollView>
下面是相应的样式定义:
<resources>
<style name="ButtonStandard" parent="@android:style/Widget.Button">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">fill_parent</item>
</style>
<style name="EditTextStandard" parent="@android:style/Widget.EditText">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">2dip</item>
<item name="android:layout_marginRight">2dip</item>
<item name="android:layout_marginTop">2dip</item>
<item name="android:layout_width">fill_parent</item>
</style>
<style name="TextViewStandard" parent="@android:style/Widget.TextView">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">2dip</item>
<item name="android:layout_marginRight">2dip</item>
<item name="android:layout_marginTop">2dip</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:textColor">@android:color/white</item>
</style>
</resources>
包装内容
填补家长的空缺
包装内容
2dip
2dip
2dip
填补家长的空缺
包装内容
2dip
2dip
2dip
填补家长的空缺
@android:彩色/白色
这会因为涉及复选框而变得非常混乱
我的定义怎么了
非常感谢。
HJWandroid表中的列始终调整为任何行中最宽的视图。这里没有列的显式控件。网格视图可以更好地控制列大小,例如:android:columnWidth=“100dp”
如果将表格中的左视图和右视图设置为相同的大小,则居中表格可以有两个间距为50%的列 您是否尝试在子视图中将
android:layout\u width
属性设置为0dp
(当然,将android:layout\u weight
保留为1
)呢
我也遇到过类似的情况,当时我也想在两个子视图之间平均分配给定的空间,但没有做到,因为具有“更广泛内容”的子视图在其父视图中也比其兄弟视图更广泛。这是因为越宽的孩子初始宽度越大。确保两个子项都从相同的宽度开始(
android:layout\u width=“0dp”
)也保证在它们之间均匀分配空间。我将创建一个表布局
在包含控件的框架布局内。在本例中,我有两列布局,并将两个ImageView置于每列的中心
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_wedding_day_cheat_sheet"
android:id="@+id/buttonWeddingDayCheatSheet"
android:onClick="onClickWeddingDayCheatSheet"
android:layout_gravity="center_horizontal">
</ImageView>
</FrameLayout>
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_share_favorite_recipe"
android:id="@+id/buttonShareFavoriteRecipe"
android:onClick="onClickShareFavoriteRecipe"
android:layout_gravity="center_horizontal">
</ImageView>
</FrameLayout>
</TableRow>
</TableLayout>
这就是我如何均匀设置表格格式的方法。不是问题的一部分,但要跨越其他不包含android:layout\u column属性的TableRow子级上的所有列android:layout\u span=“2”。希望这有帮助
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_column="0">
</TextView>
<TextView
android:id="@+id/textView2"
android:layout_column="1">
</TextView>
</TableRow>
</TableLayout>
更重要的是,如果执行此操作,请转储表格布局
并切换到线性布局
TableLayout
专为需要Android根据单元格内容计算列宽的情况而设计。如果你故意忽略了这一点,你最好使用嵌套的线性布局来代替它。@ChristianB:谢谢你的解释。我一直认为桌子的布局一定有问题。有趣的是,许多失败的TableLayouts在2.3 emulator中工作得非常完美@dbm:布局宽度设置为0dp时,所有子视图(TextView/EditText)消失。无论如何,谢谢你。我将检查Commonware的解决方案,并完全删除TableLayouts。如果你需要知道它们上面可能印着什么,可能会把它们弄得一团糟的话,你就不知道它们有什么用…@commonware:真不敢相信我为什么要拿桌面布局?将它们改为线性布局效果很好。非常感谢,这对我很有效。不过,我确实需要在FrameLayout元素中添加垂直中心。在这种情况下,可以将android:stretchColumns=“0,1”替换为android:stretchColumns=“*”