Reputation: 4370
I want to create a widget which has number of buttons which will be scaled horizontally when screen size is changed. Can I do it with single layout.xml?
I tried to illustrate it with the image below :
Upvotes: 0
Views: 47
Reputation: 24848
// try this way hope this will help you...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_margin="5dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button2"
android:layout_margin="5dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_margin="5dp"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button4"
android:layout_margin="5dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 8251
You can achieve it by setting android:layout_weight=""
tag in LinearLayout
. See the below example.
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/crop"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/crop_drawable" >
</Button>
<Button
android:id="@+id/rotate"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/rotate_drawable" >
</Button>
<Button
android:id="@+id/auto_correct"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/autocorrect_drawable" >
</Button>
<Button
android:id="@+id/add_colors"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/addcolor_drawable" >
</Button>
<Button
android:id="@+id/adjust_saturation"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/adjustsaturation_drawable" >
</Button>
<Button
android:id="@+id/photo_filter"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/photofilter_drawable" >
</Button>
</LinearLayout>
In the above example, there are 6 Buttons
which will accommodate the whole screen exactly of same size irrespective of the Screen size.
Upvotes: 0