【问题标题】:Layout 4 Buttons in Android在 Android 中布局 4 个按钮
【发布时间】:2013-06-02 15:05:55
【问题描述】:

我对 Android 开发还很陌生。我目前正在开发一个应用程序,我需要在屏幕上布局 4 个按钮。按钮 1 + 2 应该在第一行,3 + 4 在第二行。我希望每个按钮的高度和宽度相同。由于那里有多种屏幕尺寸,我希望按钮宽度为屏幕宽度的 40%。 是否可以仅通过布局来制作这样的东西,还是我需要在代码中计算所有内容?

注意:我想将应用部署到运行 Android 2.2 或更高版本的设备上。

这是一个示例图形。

编辑:对于所有正在寻找方形东西的人。这是解决方案:http://android-layouts.com/category/tags/square

【问题讨论】:

    标签: android layout android-2.2-froyo


    【解决方案1】:

    试试这个

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:text="Button" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:text="Button" />
    
        </LinearLayout>
    
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
        <Button
            android:id="@+id/button3"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:text="Button" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:text="Button" />
    
        </LinearLayout>
    
    
    
    </LinearLayout>
    

    所以为了解决这个问题,你必须使用android:layout_weight="40/100"="0.4"
    在此之前,您必须将 android:layout_width 设置为 0。因为没有它 android:layout_weight="40/100"="0.4" 不起作用。
    有关 ``android:layout_weight` 的更多详细信息,请转到
    What does android:layout_weight mean?

    UPDATE 1

    为了完成这个按钮高度的任务,试试下面的代码

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:layout_weight="0.6"
            android:orientation="horizontal" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="0px"
            android:layout_height="fill_parent"
            android:layout_weight="0.4"
            android:text="Button" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0px"
            android:layout_height="fill_parent"
            android:layout_weight="0.6"
            android:text="Button" />
    
        </LinearLayout>
    
    
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:layout_weight="0.4"
            android:orientation="horizontal" >
    
        <Button
            android:id="@+id/button3"
            android:layout_width="0px"
            android:layout_height="fill_parent"
            android:layout_weight="0.4"
            android:text="Button" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="0px"
            android:layout_height="fill_parent"
            android:layout_weight="0.6"
            android:text="Button" />
    
        </LinearLayout>
    
    
    
    </LinearLayout>
    

    【讨论】:

    • 嗨,谢谢!那行得通。如果我想让按钮的高度和宽度一样,我必须在代码中这样做吗?
    • 我已经在这个链接的帮助下解决了平方问题:android-layouts.com/category/tags/square :)
    【解决方案2】:

    正如Romain Guy 在此answer 中指出的那样:

    您不能使用百分比来定义相对布局中视图的尺寸。最好的方法是使用 LinearLayout 和权重,或自定义布局。

    所以您正在寻找的是LinearLayoutandroid:layout_weight 属性。

    以下代码将根据您的屏幕截图创建布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4" />
        </LinearLayout>
    
    </LinearLayout>
    

    结果:

    【讨论】:

      【解决方案3】:

      我会开始将前两个按钮放置在水平线性布局中,第二个按钮 2 放在另一个水平线性布局中。然后为按钮添加填充以分隔它们。至于 40%,也许您可​​以使用 weight XML 属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-03
        • 2013-03-09
        • 2011-12-17
        • 1970-01-01
        • 1970-01-01
        • 2015-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多