why is my layout_weight not working?

前端 未结 3 1237
无人共我
无人共我 2021-01-28 12:48

I have this xml file.I can\'t understand why my second linearlayout which contains editText and callButton isn\'t looking right. It looks like this (the line with the part pictu

相关标签:
3条回答
  • 2021-01-28 12:54

    Try setting the layout_width of the EditText and the ImageButton to 0dp. I also recommend you to put the attribute android:scaleType="fitCenter" in the ImageButton so that the image fits inside the button and it doesn't look bigger like in the screenshot you provided.

    Here is the XML file:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:paddingBottom="0dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="0dp"
        tools:context=".MainActivity">
    
        <ListView
            android:id="@+id/ListView_2"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1">
        </ListView>
    
        <LinearLayout
            android:id="@+id/LinearLayout2"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            >
            <!--android:adjustViewBounds="true"-->
            <!-- we want the text to begin in the centre of the edittext view
            that's what gravity does-->
    
            <EditText
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/editText"
                android:inputType="phone"
                android:gravity="center"
                android:hint="Edit Text"
                android:background="#d9d9d9"
                android:layout_weight="1"
    
                />
    
    
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:id="@+id/callButton"
                android:src="@android:drawable/sym_call_incoming"
                android:clickable="true"
                android:scaleType="fitCenter"
                android:onClick="softkeyboardButton"
                android:background="#ffa62b"
                android:layout_weight="1"
    
                />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal" >
    
            <CheckBox
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:background="#ffa500"
                android:text="New CheckBox"
                android:id="@+id/checkBox"
                android:layout_weight="4"
                />
    
    
            <ImageButton
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:id="@+id/imageButton"
                android:src="@android:drawable/ic_menu_gallery"
                android:clickable="true"
                android:onClick="softkeyboardButton"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1"
                android:background="@android:drawable/ic_menu_gallery"
                />
    
            <View android:layout_height="fill_parent"
                android:layout_width="2px"
                android:background="#90909090"/>
    
            <ImageButton
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:id="@+id/imageButton2"
                android:src="@android:drawable/ic_menu_set_as"
                android:background="@android:drawable/ic_menu_gallery"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    And this is how it looks:

    I hope it helps!

    0 讨论(0)
  • 2021-01-28 13:02

    Change your EditText and Call button with layout_width to "0dp" -

    <EditText
    

    android:layout_width="0dp"

            android:layout_height="match_parent"
            android:id="@+id/editText"
            android:inputType="phone"
            android:gravity="center"
            android:background="#d9d9d9"
            android:layout_weight="1"
    
            />
    
    
            <ImageButton
    

    android:layout_width="0dp"

                android:layout_height="match_parent"
                android:id="@+id/callButton"
                android:src="@drawable/call"
                android:clickable="true"
                android:onClick="softkeyboardButton"
                android:background="#ffa62b"
                android:layout_weight="1"
    
                />
    
    0 讨论(0)
  • 2021-01-28 13:17

    For layout_weight to work, you should set either the layout_height or layout_weight to be 0dp on the widgets with weight.

    From the looks of things I think you should spend some more time looking into how layout_weight works exactly

    https://developer.android.com/guide/topics/ui/layout/linear.html#Weight

    0 讨论(0)
提交回复
热议问题