android - Using RelativeLayout instead of Nested Weights -
i've been working on simple soundboard app. basic design imageview title, followed 4 rows of buttons (3 in each row), makes similar old cellphone's buttons. @ bottom, there "random" , "stop" button. take less height space rows mentioned above, random button double width fill space.
to achieve look, have been using nested weights , linear layouts. however, upon further research, have found bad performance because in current format, each item being measured 8 times. xml layout code below. have excluded lot of ui modifiers such margins, text, , shadows on buttons make code smaller. code may seem long, repetitive , simple understand.
<?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" android:weightsum="23" > <!-- insert title/picture in linear layout below --> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="9" android:background="@drawable/etho_logo" android:orientation="horizontal" > </linearlayout> <!-- insert sounds/buttons in linear layout below --> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="5" android:orientation="vertical" android:weightsum="4" > <!-- 1st line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightsum="3" > <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> </linearlayout> <!-- 2nd line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightsum="3" > <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> </linearlayout> <!-- 3rd line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightsum="3" > <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> </linearlayout> <!-- 4th line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" android:weightsum="3" > <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> </linearlayout> </linearlayout> <!-- random button , stop button below here --> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="9" android:orientation="horizontal" android:weightsum="3" > <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> </linearlayout> </linearlayout>
i new android, though have tried using relative layout, have had extremely hard time working it. tried watching tutorials online, still never looked right when used in code. example, title image has space above , below actual text want display. in linear layout weights, automatically become smaller fit space gave it. not in relativelayout though.
i guess i'm trying ask here is: how make relativelayout items evenly spaced buttons across few rows (as shown in example above), , how make imageviews , other things automatically size down space give them? if more information needed, please don't hesitate ask. thanks.
you right relativelayout
difficult make tables such 1 made above. linearlayout
ideal that, why tablelayout
, tablerow
view groups subclasses of linearlayout
. can recommend couple of alternatives besides relativelayout
increase layout efficiency, however.
first, can eliminate second vertically oriented linearlayout
, change layout weight of first 4 rows of buttons 1 1.25. reduce number of layout passes 8 4, best can table.
<?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" android:weightsum="23" > <!-- insert title/picture in linear layout below --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:background="@drawable/etho_logo" android:orientation="horizontal" > </linearlayout> <!-- 1st line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.25" android:orientation="horizontal" android:weightsum="3" > <!-- 3 buttons here --> </linearlayout> <!-- 2nd line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.25" android:orientation="horizontal" android:weightsum="3" > <!-- 3 buttons here --> </linearlayout> <!-- 3rd line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.25" android:orientation="horizontal" android:weightsum="3" > <!-- 3 buttons here --> </linearlayout> <!-- 4th line of buttons --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.25" android:orientation="horizontal" android:weightsum="3" > <!-- 3 buttons here --> </linearlayout> <!-- random button , stop button below here --> <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:orientation="horizontal" android:weightsum="3" > <button android:id="@+id/bintro" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> <button android:id="@+id/bintro" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/hello_real_select" /> </linearlayout> </linearlayout>
another option use single layout makes "flat" grid (a viewgroup
manages layout of children table itself). if targeting api level 14+ app, can @ gridlayout
. if targeting api level prior 14, might have make own. on final note, please not confuse gridlayout
gridview
view group, meant used lists , not accomplish want.
Comments
Post a Comment