为什么我的 layout_weight 不工作?

why is my layout_weight not working?

我有这个 xml file.I 无法理解为什么我的第二个包含 editText 和 callButton 的线性布局看起来不正确。它看起来像这样(phone部分图片的那一行):

我在每个视图、editText 和 callButton 中都将 layout_weights 设置为 1,所以 phone 图像应该占据屏幕的一半,对吗?这是我的代码。我希望这两个视图占用相同的宽度 - 并且当输入文本时 editText 不应拉伸,就像当前正在发生的那样。感谢您的帮助。

<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="wrap_content"
        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="wrap_content"
            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"

            />
</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="@drawable/softkeyboard"
            android:clickable="true"
            android:onClick="softkeyboardButton"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:background="@drawable/buttonstates"
             />

        <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="@drawable/settingsicon"
            android:background="@drawable/buttonstates"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1" />

    </LinearLayout>

    </LinearLayout>

要使 layout_weight 正常工作,您应该 将小部件上的 layout_heightlayout_weight 设置为 0dp有重量。

从外观上看,我认为您应该花更多时间研究 layout_weight 的确切工作原理

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

将 layout_width 的 EditText 和 Call 按钮更改为“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"

            />

尝试将 EditText 和 ImageButton 的 layout_width 设置为 0dp。 我还建议您将属性 android:scaleType="fitCenter" 放在 ImageButton 中,这样图像就适合按钮,并且看起来不像您提供的屏幕截图那样大。

这是 XML 文件:

<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>

这是它的样子:

希望对您有所帮助!