将片段宽度设置为屏幕宽度的百分比

KNS*_*KNS 2 android android-layout android-fragments

Fragment需要从屏幕的底部到顶部,从左边缘到屏幕的80%.

我已经尝试使用android:layout_weight="0.8"我得到'无效的布局参数'消息,即使我嵌套<fragment>在另一个<RelativeLayout><LinearLayout>

现在,高度从上到下跨越,因为android:layout_height="fill_parent" 但宽度仍然设置为wrap_content因为我不知道如何设置是80%的屏幕宽度.

<fragment
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/xFragment"
        android:name="com.example.x.x.xFragment"/>
Run Code Online (Sandbox Code Playgroud)

谢谢

Ale*_*nez 5

将您的片段放在线性布局中,如下所示:

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

    <fragment
            android:layout_width="0dp"
            android:layout_weight="8"
            android:layout_height="fill_parent"
            android:id="@+id/xFragment"
            android:name="com.example.x.x.xFragment"/>


     <View
            android:id="@+id/otherElement"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"/>

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

希望它能帮到你!!

  • 如果答案对你有用,你能标记upvote ?? @ KNS (2认同)