我正在尝试创建菜单布局,我可以将按钮放在宽度相同的两列中。
我不想伸展按钮来填满整个屏幕(除非有很长的标签)。
我希望所有的按钮都有相同的宽度-标签最长的按钮的宽度。
我尝试在一个水平线性布局中使用两个垂直线性布局。
我发现的所有解决方案都建议对垂直线性布局使用相同的layout_weight,对水平布局使用layout_width=fill_parent,这会迫使按钮占据整个屏幕宽度。
对于我的问题有什么通用的解决方案吗?
这就是我所期望的-- https://dl.dropbox.com/u/2751770/android-layout1.PNG
下面是常见的解决方案-- https://dl.dropbox.com/u/2751770/android-layout2.PNG
下面是我常用的解决方案的代码:
<LinearLayout style="@style/menuLayout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<Button style="@style/menuButton"
android:text="Title 1"/>
<Button style="@style/menuButton"
android:text="Title 2"/>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<Button style="@style/menuButton"
android:text="Title 3"/>
<Button style="@style/menuButton"
android:text="Long title 4"/>
</LinearLayout>
</LinearLayout>样式:
<style name="menuLayout">
<item name="android:layout_gravity">center</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
</style>
<style name="menuButton">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:paddingLeft">30dp</item>
<item name="android:paddingRight">30dp</item>
<item name="android:layout_margin">5dp</item>
</style>发布于 2012-12-03 23:37:14
您在样式"menu_layout“中设置了宽度,并且在code.Remove的第二行中也设置了宽度。并尝试在父LinearLayout中使用android:weightSum="1",然后在每个LinearLayout中使用android:layout_weight="0.5"。
https://stackoverflow.com/questions/12332592
复制相似问题