简体   繁体   English

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

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

I am having a big problem to create an Android Layout, that contains only four ImageButtons. 我在创建仅包含四个ImageButton的Android布局时遇到了一个大问题。

It should look like this: 它看起来应该像这样: https://cldn0.fiverrcdn.com/fiverr/t_main1/gigs/3008268/original/minimal-trend_0.jpg

At the moment I created a TableLayout with two LinearLayout (horizontal: each contains two buttons.) But I am doing this by set the exact values: 目前,我用两个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"
            />

Is there a better way to do this? 有一个更好的方法吗? (more dynamically for other screen sizes without setting the width and height to an value) (对于其他屏幕尺寸,更动态地设置宽度和高度为一个值)

Try something like: 尝试类似:

    <?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>

Explanation 说明

Here we use the weight attribute which divides the screen in percentages depending on the weighs. 在这里,我们使用weight属性,该属性根据重量将屏幕划分为百分比。 For example, putting 1 in both children it will make them to take the 50% of the screen. 例如,在两个孩子中都放入1,将使他们占据屏幕的50%。 That is what we are doing first in vertical, and then in horizontal for each button. 这就是我们首先在垂直方向上对每个按钮进行水平操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM