【问题标题】:How to set right view width to wrapcontent and left view width to remaining space如何设置右视图宽度以包装内容并将左视图宽度设置为剩余空间
【发布时间】:2015-01-20 19:16:57
【问题描述】:

您好,我有一个水平方向的 LinearLayout,其中有两个按钮。我想将右侧按钮宽度设置为 wrap_content 并将左侧按钮宽度设置为剩余宽度。

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:weightSum="2">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" 
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:layout_weight="1"/>

</LinearLayout>

有没有办法使用它的 weight_sum 和 weight 属性?

提前致谢。

【问题讨论】:

  • 首先将 android:orientation="vertical" 改为 android:orientation="horizo​​ntal"。是的,你使用 android:weightSum="1"

标签: android


【解决方案1】:

有没有办法使用它的 weight_sum 和 weight 属性?

答案是:weight 属性 - 是的,weight_sum 属性 - 没有。

试试这个:

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

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button1" 
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"/>

</LinearLayout>

但是您需要指定整个布局的width(作为精确值或作为match_parent;宽度,因为您写它应该是水平布局)

【讨论】:

    【解决方案2】:

    你不需要两个都用,试试吧:

     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    
            android:text="Button2" />
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      您可以使用权重按比例设置宽度。如果您知道左侧按钮相对于另一个按钮占用了多少空间,则可以使用重量。

      【讨论】:

        【解决方案4】:

        试试这个:-

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">
        
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button1"
            android:layout_weight="2"/>
        
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:layout_weight="0"/>
        
        </LinearLayout>
        

        希望对你有帮助:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-09
          • 2018-10-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多