<include>标记中的layout_weight属性

se.*_*yev 10 android

我正在尝试使用<include>标记在android应用程序中重用一些布局组件.我有不同的港口和土地布局:

  1. 港口:

    <LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
    
        <include layout="@layout/calc_equals_button" a:layout_weight="4"/>
        <include layout="@layout/calc_display"/>
    
    </LinearLayout>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 土地:

    <LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
    
        <include layout="@layout/calc_equals_button"/>
        <include layout="@layout/calc_display"/>
    
    </LinearLayout>
    
    Run Code Online (Sandbox Code Playgroud)

主要的区别是a:layout_weight="4",所以我希望我的calc_equals_button组件在端口方向上更小.

问题是,如果我尝试calc_equals_button直接嵌入组件一切正常,例如:

<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
        <DirectionDragButton
                xmlns:a="http://schemas.android.com/apk/res/android"
                a:id="@+id/equalsButton"
                a:text="="
                a:layout_width="match_parent"
                a:layout_height="match_parent"
                a:layout_weight="4"
                style="@style/control_button_style"
                a:onClick="numericButtonClickHandler"/>

        <include layout="@layout/calc_display"/>

    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

否则 - 不是.

以下是calc_equals_button.xml的示例:

<DirectionDragButton
    xmlns:a="http://schemas.android.com/apk/res/android"
    a:id="@+id/equalsButton"
    a:text="="
    a:layout_width="match_parent"
    a:layout_height="match_parent"
    style="@style/control_button_style"
    a:onClick="numericButtonClickHandler"/>
Run Code Online (Sandbox Code Playgroud)

Rom*_*Guy 26

当前的限制是您必须为要应用的其他layout_*属性指定layout_width和layout_height.

  • @ se.solovyev它解决了您的问题...您的包含标签必须指定要考虑的重量的宽度和高度. (4认同)