【问题标题】:Android buttons width in proportionAndroid 按钮宽度成比例
【发布时间】:2012-07-19 13:23:47
【问题描述】:

我需要在一行中放置 3 个按钮。/使用 LinearLayout/。 问题是在按钮上更改文本时。由于“android:layout_width =“wrap_content”,宽度会发生变化。我想确保thab按钮将占据屏幕宽度i 4:4:2的比例。子句'wrap_content'打破了这一点,但我不能使用确切的尺寸,因为我没有'在设计时不知道设备屏幕宽度。

有人知道不用代码就能轻松解决这个问题吗?

【问题讨论】:

  • 对所有按钮使用 layout_weight=1

标签: android layout


【解决方案1】:

不要将它们的宽度设置为 wrap_content。改为设置比例权重:

<LinearLayout
    ...
    layout_width = "match_parent"
    layout_height = "wrap_content" />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 2 />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 2 />

    <Button
        ....
        layout_width = "0dp"
        layout_height = "wrap_content"
        layout_weight = 1 />

</LinearLayout>

【讨论】:

  • 这在我的手机上运行良好。还是得用不同的设备试试。
  • 太棒了!它也应该共享其他设备中的空间。
【解决方案2】:

尝试以下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:weightSum="10"   //    SEE THIS IS 4+4+2 = 10
        android:layout_centerHorizontal="true" >

        <Button
            android:layout_weight="4"   // THIS    IS   4
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:layout_weight="4"   // THIS    IS   4
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:layout_weight="2"   // THIS    IS   2
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

</RelativeLayout>

【讨论】:

  • 这正是我所做的。由于按钮标签 - 系统调整了我的按钮大小并破坏了比例。
  • 如果你将宽度设置为 0dp 那么你必须给定高度。你可以给定高度吗?
  • 你怎么给的高度?文本长度的大小变化时不会变化吗?
  • android:layout_height="wrap_content"
  • 当我设置 height="wrap_content" 并添加更大的文本然后按钮的大小然后按钮的高度变化。
【解决方案3】:

应该是 android:layout_weight="4dp"

【讨论】:

    猜你喜欢
    • 2013-08-05
    • 2023-03-05
    • 2015-04-22
    • 2017-12-05
    • 2015-03-02
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多