Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何制作相同大小的按钮_Android_Xml_Android Layout_Android Studio_Layout - Fatal编程技术网

Android 如何制作相同大小的按钮

Android 如何制作相同大小的按钮,android,xml,android-layout,android-studio,layout,Android,Xml,Android Layout,Android Studio,Layout,这是我的xml,这是结果,但如果上面的3个按钮(一起)占据了最大按钮的相同大小,类似于该图像,我应该在哪里更改布局 这就是我所拥有的: 这就是我想要的: 我的xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_p

这是我的xml,这是结果,但如果上面的3个按钮(一起)占据了最大按钮的相同大小,类似于该图像,我应该在哪里更改布局

这就是我所拥有的:

这就是我想要的:

我的xml:

<?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:background="@android:color/transparent"
              android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#FFA500"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="#FFA500"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textoPoup1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="1. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
                android:textColor="@android:color/white"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/nota0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"/>

                <Button
                    android:id="@+id/nota40"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="50"/>

                <Button
                    android:id="@+id/nota80"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="90"/>
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/nota120"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="110"/>

                <Button
                    android:id="@+id/nota160"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="170"/>

                <Button
                    android:id="@+id/nota200"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="300"/>
            </LinearLayout>

            <Button
                android:id="@+id/proxima1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Next"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

对于以下按钮,可以使用
layout\u weight
轻松完成此操作:

<Button
        android:id="@+id/nota0"
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="0"/>
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/nota0"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="0"/>

            <Button
                android:id="@+id/nota40"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="50"/>

            <Button
                android:id="@+id/nota80"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="90"/>
        </LinearLayout>

这样,每个按钮将恰好占据其父按钮宽度的1/3,这将占据整个屏幕宽度。您仍然可以根据需要使用边距和填充来调整按钮之间的间距

编辑
使用
ConstraintLayout
可以更轻松地执行相同的操作,而无需嵌套、使用和应用水平或垂直权重,如
app:layout\u constraintHorizontal\u weight=“1”

线性布局中的3个添加按钮

android:orientation="horizontal"
现在,为所有三个按钮设置以下内容:

android:layout_width="0dp"
android:layout_weight="1"
大概是这样的:

<Button
        android:id="@+id/nota0"
        android:layout_width="0"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="0"/>
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/nota0"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="0"/>

            <Button
                android:id="@+id/nota40"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="50"/>

            <Button
                android:id="@+id/nota80"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="90"/>
        </LinearLayout>

然后您可以添加适当的页边距:

对于第一个按钮,仅将leftMargin添加为xdp,将rightMargin添加为x/2dp。 对于第二个按钮,添加左右边距作为xdp。
对于第三个按钮,将左边距添加为x/2dp,将右边距添加为xdp。

在linearlayout中,我们可以为linearlayout中的每个子元素提供布局权重属性,该属性用于每个子元素的大小权重,如果layout\u width=“0dp”,它将根据权重或如果layout\u height=“0dp”计算该元素的宽度它将根据重量计算该元件的高度

它取总元素重量的平均值,即如果所有元素都有布局_weight=“1”,它将分配其父元素宽度的1/3

因此,在您的情况下,您需要相同的按钮大小,因此需要为所有元素和布局分配相同的权重。\u width=“0dp”



尝试使用布局\每个小按钮都应该是这样的:嵌套布局是一种反模式,因为它不利于性能。@BobMalooga我一直想知道如何在不嵌套的情况下获得所需的外观(我已经非常习惯于使用线性布局,主要是偶尔使用框架/相对物)?@BobMalooga哇。。。只是哇!男孩,你让我开心。谢谢你,伙计!