简体   繁体   English

Android线性布局无法正确缩放百分比

[英]Android Linear Layout not scaling percentages properly

I'm trying to create a simple calculator. 我正在尝试创建一个简单的计算器。 What I have is a vertical linear layout that contains bunch of other horizontal linear layouts. 我所拥有的是一个垂直线性布局,其中包含许多其他水平线性布局。 In each horizontal layout, there are 5 elements. 在每个水平布局中,都有5个元素。 However, in one of them, there is 4 instead of 5, so what I thought I could do is scale of the buttons to twice the size of the other but having it occupy 40% of the screen. 但是,在其中一个中,有4个而不是5个,所以我想我可以做的是将按钮的比例缩放为另一个按钮的两倍,但它却占据了屏幕的40%。 But the results are not as I want them. 但是结果却不是我想要的。 Here is my layout code: 这是我的布局代码:

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

            <Button
                android:id="@+id/negative"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/buttonshape"
                android:minHeight="63dp"
                android:minWidth="63dp"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="1"
                android:text="+/-"
                android:textColor="#FFFFFF"
                android:textSize="30sp" />

            <Button
                android:id="@+id/zero"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/buttonshape"
                android:minHeight="63dp"
                android:minWidth="63dp"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="3"
                android:text="0"
                android:textColor="#FFFFFF"
                android:textSize="30sp" />

            <Button
                android:id="@+id/dot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/buttonshape"
                android:minHeight="63dp"
                android:minWidth="63dp"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="3"
                android:text="."
                android:textColor="#FFFFFF"
                android:textSize="30sp" />

            <Button
                android:id="@+id/equals"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="@drawable/buttonshape"
                android:minHeight="63dp"
                android:minWidth="63dp"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="3"
                android:text="="
                android:textColor="#FFFFFF"
                android:textSize="30sp" />
        </LinearLayout>

I have tried the weightSum, but that is not working either. 我已经尝试过weightSum,但这也不起作用。 在此处输入图片说明

为每个按钮设置:

 android:layout_width="0dp"

Use below Code: 使用以下代码:

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

        <Button
            android:id="@+id/negative"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/buttonshape"
            android:minHeight="63dp"
            android:minWidth="63dp"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="1"
            android:text="+/-"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />

        <Button
            android:id="@+id/zero"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/buttonshape"
            android:minHeight="63dp"
            android:minWidth="63dp"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="3"
            android:text="0"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />

        <Button
            android:id="@+id/dot"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/buttonshape"
            android:minHeight="63dp"
            android:minWidth="63dp"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="3"
            android:text="."
            android:textColor="#FFFFFF"
            android:textSize="30sp" />

        <Button
            android:id="@+id/equals"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="@drawable/buttonshape"
            android:minHeight="63dp"
            android:minWidth="63dp"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="3"
            android:text="="
            android:textColor="#FFFFFF"
            android:textSize="30sp" />
    </LinearLayout>

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

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