【问题标题】:Create Layout/Screen with four ImageButtons (all of the same size)使用四个 ImageButtons 创建布局/屏幕(大小相同)
【发布时间】:2015-04-24 18:26:08
【问题描述】:

我在创建一个仅包含四个 ImageButtons 的 Android 布局时遇到了一个大问题。

它应该看起来像这样:

目前我创建了一个带有两个 LinearLayout 的 TableLayout(水平:每个包含两个按钮。)但是我通过设置确切的值来做到这一点:

<ImageButton
            android:layout_width="178dp"
            android:layout_height="260dp"
            android:src="@drawable/personal_coach"
            android:id="@+id/button_personal_coach"
            android:layout_weight="0.25"
            android:scaleType="fitXY"
            android:background="?android:selectableItemBackground"
            android:layout_marginRight="4dp"
            />

有没有更好的方法来做到这一点? (对于其他屏幕尺寸更动态,无需将宽度和高度设置为值)

【问题讨论】:

    标签: android android-layout height imagebutton


    【解决方案1】:

    尝试类似:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="2">
    
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:weightSum="2"
            android:orientation="horizontal">
    
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
    
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:weightSum="2"
            android:orientation="horizontal">
    
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
    
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
    

    说明

    这里我们使用 weight 属性,它根据重量按百分比划分屏幕。例如,将 1 放在两个孩子中,这将使他们占据屏幕的 50%。这就是我们首先在垂直方向上做的事情,然后是每个按钮的水平方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多