简体   繁体   中英

layout_weight not adjusting size properly

I am trying to make a calculator app. I have a outer linearlayout which is vertical orientation and then have nested linear layouts which have a horizontal orientation where my buttons will go. Only the last nested linear layout is not adjusting the width properly of the buttons as I want the '0' button to take half the width and the '.' and '=' button to take a quarter of the width. I gave the '=' button a layout weight of 0.5 and the '.' & '=' button a layout weight of 0.25 but it still won't work properly.

Here is a screenshot of the graphical layout:

在此处输入图片说明

XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#000000"
android:weightSum="1">

<TextView 
    android:text="0"
    android:layout_width="wrap_content"
    android:layout_weight = "0.25"
    android:layout_height="0dp"
    android:textSize="40sp"
    android:layout_gravity="right"
    android:textColor="#FFFFFF"
    android:id="@+id/tvDisplay"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:orientation="horizontal"
    android:layout_weight="0.15"
    android:weightSum="1">

   <Button
       android:id="@+id/bClear"
       android:layout_width="wrap_content"
       android:layout_height="fill_parent"
       android:layout_margin="0.2dp"
       android:layout_weight="0.25"
       android:background="#FFFFFF"
       android:text="C"
       android:textSize="30sp" />

    <Button
        android:id="@+id/bChangeSign"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_margin="0.2dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="+/-"
        android:textSize="30sp" />

     <Button
         android:id="@+id/bPercent"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
         android:background="#FFFFFF"
         android:text="%"
         android:textSize="30sp" />

     <Button
         android:id="@+id/bDivide"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
         android:background="#FF9900"
         android:text="/"
         android:textSize="30sp" />

</LinearLayout>

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:orientation="horizontal"
    android:layout_weight="0.15">

    <Button 
        android:text="7"
        android:background="#FFFFFF"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_margin="0.2dp"
        android:layout_weight="0.25"
        android:id="@+id/b7"/>

   <Button 
        android:text="8"
        android:background="#FFFFFF"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_margin="0.2dp"
        android:layout_weight="0.25"
        android:id="@+id/b8"/>

    <Button 
        android:text="9"
        android:background="#FFFFFF"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_margin="0.2dp"
        android:layout_weight="0.25"
        android:id="@+id/b9"/>

    <Button
        android:id="@+id/bMultiply"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_margin="0.2dp"
        android:layout_weight="0.25"
        android:background="#FF9900"
        android:text="x"
        android:textSize="30sp" />

</LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:orientation="horizontal"
    android:layout_weight="0.15">

   <Button 
        android:text="4"
        android:background="#FFFFFF"
        android:textSize="30sp"
        android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/b4"/>

    <Button 
        android:text="5"
        android:background="#FFFFFF"
        android:textSize="30sp"
        android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/b5" />

     <Button 
        android:text="6"
        android:background="#FFFFFF"
        android:textSize="30sp"
       android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/b6" />

     <Button 
        android:text="-"
        android:background="#FF9900"
        android:textSize="30sp"
        android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/bSubtract"
        />

</LinearLayout>

      <LinearLayout
   android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:orientation="horizontal"
    android:layout_weight="0.15">

    <Button 
        android:text="1"
        android:background="#FFFFFF"
        android:textSize="30sp"
        android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/b1" />

   <Button 
        android:text="2"
        android:background="#FFFFFF"
        android:textSize="30sp"
       android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/b2" />

    <Button 
        android:text="3"
        android:background="#FFFFFF"
        android:textSize="30sp"
        android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/b3" />

     <Button 
        android:text="+"
        android:background="#FF9900"
        android:textSize="30sp"
       android:layout_width="wrap_content"
         android:layout_height="fill_parent"
         android:layout_margin="0.2dp"
         android:layout_weight="0.25"
        android:id="@+id/bPlus" />

</LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.15"
            android:orientation="horizontal" 

            <Button
                android:id="@+id/b0"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_margin="0.2dp"
                android:layout_weight="0.5"
                android:background="#FFFFFF"
                android:text="0"
                android:textSize="30sp" />

            <Button
                android:id="@+id/bDecimal"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_margin="0.2dp"
                android:layout_weight="0.25"
                android:background="#FFFFFF"
                android:text="."
                android:textSize="30sp" />

            <Button
                android:id="@+id/bEquals"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_margin="0.2dp"
                android:layout_weight="0.25"
                android:background="#FF9900"
                android:text="="
                android:textSize="30sp" />
        </LinearLayout>

The solution to this problem is remarkably simple.

Android is great, and the tools for developing it are great, but then you come across behaviors like this and you realize that it certainly isn't perfect yet.

Go ahead and change the android:layout_width in your 3 buttons at the bottom to

android:layout_width="0dp"

Now, this was the way this was explained to me. The layout_weight attribute tries to balance out the widths to be in the correct ratios, but it takes into account the widths of the buttons it is trying to balance. By setting the width to absolutely nothing (0dp), you're taking away any sort of widths that may have interfered with layout_weigth 's attempts to balance the ratios. Basically, so that there are no other values that could throw off the shifting of the widths.

I hope that helps. Good luck :)

test well

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:weightSum="1" >

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_gravity="right"
    android:layout_weight="0.25"
    android:text="0"
    android:textColor="#FFFFFF"
    android:textSize="40sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.15"
    android:orientation="horizontal"
    android:weightSum="1" >

    <Button
        android:id="@+id/bClear"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="C"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bChangeSign"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="+/-"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bPercent"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="%"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bDivide"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FF9900"
        android:text="/"
        android:textSize="30sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.15"
    android:orientation="horizontal"
    android:weightSum="1" >

    <Button
        android:id="@+id/b7"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="7"
        android:textSize="30sp" />

    <Button
        android:id="@+id/b8"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="8"
        android:textSize="30sp" />

    <Button
        android:id="@+id/b9"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="9"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bMultiply"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FF9900"
        android:text="x"
        android:textSize="30sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.15"
    android:orientation="horizontal"
    android:weightSum="1" >

    <Button
        android:id="@+id/b4"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="4"
        android:textSize="30sp" />

    <Button
        android:id="@+id/b5"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="5"
        android:textSize="30sp" />

    <Button
        android:id="@+id/b6"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="6"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bSubtract"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FF9900"
        android:text="-"
        android:textSize="30sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.15"
    android:orientation="horizontal"
    android:weightSum="1" >

    <Button
        android:id="@+id/b1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="1"
        android:textSize="30sp" />

    <Button
        android:id="@+id/b2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="2"
        android:textSize="30sp" />

    <Button
        android:id="@+id/b3"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="3"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bPlus"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FF9900"
        android:text="+"
        android:textSize="30sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.15"
    android:orientation="horizontal"
    android:weightSum="1" >

    <Button
        android:id="@+id/b0"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.5"
        android:background="#FFFFFF"
        android:text="0"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bDecimal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FFFFFF"
        android:text="."
        android:textSize="30sp" />

    <Button
        android:id="@+id/bEquals"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="0.1dp"
        android:layout_weight="0.25"
        android:background="#FF9900"
        android:text="="
        android:textSize="30sp" />
</LinearLayout>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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