1

好的,所以我需要一个按钮来始终占据线性布局的 25%,无论里面有什么。其他 75% 将是 TextView 字段。这是我到目前为止所拥有的:

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="0dp "
        android:layout_weight=".25"
        android:text="" />

    <EditText
        android:id="@+id/edit_message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight=".75"
        android:hint="@string/edit_message" />
</LinearLayout>

然而,在 0dp 按钮消失,并且在 wrap_content 它将始终选择文本来设置宽度。如何强制它使用权重作为设置宽度的方法。

4

4 回答 4

3

尝试这个....

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

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />

        <EditText
            android:id="@+id/edit_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:hint="@string/edit_message" />
    </LinearLayout>
于 2013-09-29T06:43:11.073 回答
1
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content "
    android:layout_weight=".75"
    android:text="" />

<EditText
    android:id="@+id/edit_message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".25"
    android:hint="@string/edit_message" />
于 2013-09-29T06:58:21.167 回答
0

您将 0dp 设置为高度,改为使用 wrapcontent

<Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content "
    android:layout_weight=".25"
    android:text="" />

<EditText
    android:id="@+id/edit_message"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".75"
    android:hint="@string/edit_message" />

于 2013-09-29T06:43:53.067 回答
0

如果您将 orientation 用作水平方向,则使用

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

<Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".25"
    android:text="" />

<EditText
    android:id="@+id/edit_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".75"
    android:hint="@string/edit_message" />

如果您将方向用作垂直,则使用

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="0dp "
    android:layout_weight=".25"
    android:text="" />

<EditText
    android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight=".75"
    android:hint="@string/edit_message" />

于 2013-09-29T07:13:17.653 回答