Android 使按钮占据线性布局的25%

Android 使按钮占据线性布局的25%,android,android-layout,Android,Android Layout,好的,所以我需要一个按钮来始终占据线性布局的25%,无论其中有什么。其余75%为文本视图字段。 以下是我到目前为止的情况: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="1"> <Button

好的,所以我需要一个按钮来始终占据线性布局的25%,无论其中有什么。其余75%为文本视图字段。 以下是我到目前为止的情况:

<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_内容时,它将始终选择文本来设置宽度。如何强制它使用权重作为设置宽度的方法

试试这个

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

试试这个

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

如果将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" />



如果将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" />




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

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

如果使用方向作为垂直方向,则使用

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



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

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

如果使用方向作为垂直方向,则使用

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



这起作用了。虽然我在“入职培训”中添加了一个weightSum=“4”。您能解释一下我为什么不添加吗?我不是想粗鲁,我只是不明白。代码如何知道3等于75%,1等于25%?它需要所有的布局权重并为你计算它们的总数吗?嗯……你可以说android为你做了……但是没有僵硬的逻辑,因为android总是在布局和适配器的情况下挑战你的逻辑……你稍后会体验到……:)这很有效。虽然我在“入职培训”中添加了一个weightSum=“4”。您能解释一下我为什么不添加吗?我不是想粗鲁,我只是不明白。代码如何知道3等于75%,1等于25%?它是否需要所有的布局权重并为你计算它们的总数?嗯……你可以说android为你做了。但是没有僵硬的逻辑,因为android总是在布局和适配器的情况下挑战你的逻辑……你稍后会体验到……)