Linear layout of several images, can't get fill_parent/wrap_content right
I have a grid of 9x2 PNG's that I would like to distribute in my widget
After some tips I decided to go for (cleaned up the code a bit)
<linearLayout
android:orientation="vertical">
<linearLayout
android:orientation="horizontal">
<ImageView />
<I开发者_如何转开发mageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
</linearLayout>
<linearLayout
android:orientation="horizontal">
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
<ImageView />
</linearLayout>
</linearLayout>
The million dollar question is how to put the width/height of the elements. I'd like the object to fill the whole width of the widget, hence width="fill_parent" on the top level. But apart from that I would just like the images to scale nicely. I can't seem to get that working and wonder if I missed the concept of fill_parent/wrap_content.
I tried exeperimenting with the weight parameter but since the images have different sizes this just didn't work out.
Any help on how to define the width/height of the above elements to make this 9x2 grid fill the width and take up as much space as neccessary on the height would be greatly appreciated!
Thanks Johan
You have to use the layout parameter android:layout_weight. If all imageviews have layout_weight=1 the space will be divided equally among them:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#0000aa">
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/>
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/>
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00aaaa">
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/>
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/>
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" />
<ImageView android:layout_weight="1" android:src="@drawable/icon" android:scaleType="fitXY" android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="#ff0000"/>
</LinearLayout>
</LinearLayout>
精彩评论