KNS
KNS

Reputation: 101

Set Width of Fragment as a percentage of screen Width

I have a Fragment I need to span from the bottom to the top of the screen, and from the left edge to 80% of the screen.

I've tried Using android:layout_weight="0.8" I get an 'Invalid layout parameter' message, even when I nest the <fragment> in another <RelativeLayout> or <LinearLayout>

Right now, the height spans from top to bottom, because of android:layout_height="fill_parent" But width is still set to wrap_content because I have no idea how to set is to 80% of screen width.

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

Thank you

Upvotes: 2

Views: 3043

Answers (2)

Sam
Sam

Reputation: 1269

Fragments do not have parameters like height, width ,margins. View and ViewGroup have parameters like height, width ,margins. So, you adjust the container into which you are placing the fragment.

Upvotes: 2

Put your fragment in a linear layout, something like this:

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

Hope it helps you!!

Upvotes: 5

Related Questions