Tuesday, 28 July 2015

android layout android:layout_weight and match_parent









To do this you need:


<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="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="com.xxx.xxx.views.ScannerActivity">

    <com.journeyapps.barcodescanner.CompoundBarcodeView        android:id="@+id/barcode_scanner"        android:layout_weight="2"        android:layout_width="match_parent"        android:layout_height="match_parent">

    </com.journeyapps.barcodescanner.CompoundBarcodeView>

    <ListView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1"        android:id="@+id/listView_xxx" />

    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:id="@+id/buttonsLayout">

        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Pause"            android:onClick="pause" />

        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="Resume"            android:onClick="resume" />
    </LinearLayout>

</LinearLayout>


Explanation:

You want all components work together and full file the layout. And you want some of them take more space some of them take less. and some of them just take the space as it size. (if you check the image i showed above, 3 things are displaying vertically on the page. Buttons's high is it own high, but listview get more than the customized view. listview and customized view doesn't have content or their content are dynamic when it running. )

And you sometime also want two components take half-half space. (buttons on the bottom)

To do this you need set layout_xxx to match_parent.

Then, it claim the component want to occupy  the whole screen,  then the first component will get the whole screen, and you can not see rest of components.

The solution of manage the layout is by setting "android:layout_weight", when components are competing for match_parent.



No comments:

Post a comment