簡體   English   中英

使用四個ImageButton(大小相同)創建布局/屏幕

[英]Create Layout/Screen with four ImageButtons (all of the same size)

我在創建僅包含四個ImageButton的Android布局時遇到了一個大問題。

它看起來應該像這樣: https://cldn0.fiverrcdn.com/fiverr/t_main1/gigs/3008268/original/minimal-trend_0.jpg

目前,我用兩個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"
            />

有一個更好的方法嗎? (對於其他屏幕尺寸,更動態地設置寬度和高度為一個值)

嘗試類似:

    <?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%。 這就是我們首先在垂直方向上對每個按鈕進行水平操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM