android - How to create layout with 6 buttons like windows tiles -
i'm trying create layout 6 buttons automatically adapt screen size tiles of windows phone. in code create dynamically 6 button, 2 line button should fit size of screen filling latter. how can proceed?
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="0dip" android:orientation="horizontal" android:weightsum="2" > <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/conv_up" /> <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/conv_up" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dip" android:orientation="horizontal" android:weightsum="2" > <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/conv_up" /> <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/conv_up" /> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dip" android:orientation="horizontal" android:weightsum="2" > <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/conv_up" /> <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/conv_up" /> </linearlayout>
i'd use vertical linearlayout
3 rows of same weight children, each row being horizontal linearlayout
having 2 children of same weights, make sure full area filled. 6 buttons performance shouldn't issue.
if performance concern, can make rows relativelayout
s , use strut split in half , position 2 children based on that.
when strut, mean this:
<view android:id="@+id/strut" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerhorizontal="true"/>
update: since you're trying linearlayout
s, here's how can deal heights , widths:
the parent linearlayout
can have:
android:layout_width="match_parent" android:layout_height="match_parent"
the 3 linearlayout
children have:
android:layout_width="match_parent" android:layout_height="0dip"
the button
s have:
android:layout_width="0dip" android:layout_height="match_parent"
as can notice, have 0dip
property weight applied on (either on height if parent vertical oriented, or width if parent horizontal oriented), need grow fill in space.
here's full xml (buttons don't include drawables, feel free add yours):
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="0dip" android:orientation="horizontal" android:layout_weight="1" > <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dip" android:orientation="horizontal" android:layout_weight="1" > <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1"/> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="0dip" android:orientation="horizontal" android:layout_weight="1" > <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <button android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> </linearlayout> </linearlayout>
and result:
Comments
Post a Comment