【问题标题】:android - views with same width placed horizontaly without filling all screenandroid - 具有相同宽度的视图水平放置而不填满整个屏幕
【发布时间】:2012-09-08 16:44:01
【问题描述】:

我正在尝试创建菜单布局,我可以在其中将按钮放置在具有相同宽度的两个列中。
我不想拉伸按钮以填满整个屏幕(除非有长标签)。
我希望所有按钮都具有相同的宽度——标签最长的按钮的宽度。
我正在一个水平线性布局中尝试两个垂直线性布局。
我发现的所有解决方案都建议对垂直线性布局使用相同的 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>

【问题讨论】:

  • 其实你能说清楚你的问题到底是什么吗?现在发生了什么?并提供屏幕截图并将您的代码粘贴到此处,以便我们改进它

标签: android layout


【解决方案1】:

您正在设置样式“menu_layout”中的宽度,并且再次在代码的第二行中设置宽度。删除其中一个。并尝试在父级LinearLayout 中使用android:weightSum="1",然后在每个LinearLayout 中使用android:layout_weight="0.5"

【讨论】:

    猜你喜欢
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2012-04-02
    • 1970-01-01
    相关资源
    最近更新 更多