简体   繁体   English

layout_weight无法正确调整大小

[英]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. 我有一个外部的linearlayout ,它是垂直方向,然后有嵌套的线性布局,其中的水平方向是我的按钮可以通过的地方。 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 '.' 只有最后嵌套的线性布局无法正确调整按钮的宽度,因为我希望“ 0”按钮占一半宽度,而“。”占一半。 and '=' button to take a quarter of the width. 和“ =”按钮占据宽度的四分之一。 I gave the '=' button a layout weight of 0.5 and the '.' 我给'='按钮的布局权重为0.5,而'。 & '=' button a layout weight of 0.25 but it still won't work properly. &'='按钮的布局权重为0.25,但仍无法正常工作。

Here is a screenshot of the graphical layout: 这是图形布局的屏幕截图:

在此处输入图片说明

XML code: XML代码:

<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. Android很棒,开发它的工具也很棒,但是随后您遇到了这样的行为,您意识到它当然还不是完美的。

Go ahead and change the android:layout_width in your 3 buttons at the bottom to 继续,将底部的3个按钮中的android:layout_width更改为

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. layout_weight属性会尝试平衡宽度,以达到正确的比例,但是会考虑要平衡的按钮的宽度。 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. 通过将宽度设置为绝对没有(0dp),您可以消除可能干扰layout_weigth平衡比例的任何宽度。 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>

在此处输入图片说明

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

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