Steve P.
Steve P.

Reputation: 14699

Experimenting with xmls for android UI. Would like to know if there's a better way to accomplish my task

Here's the current main screen for a toy project that I'm working on (.xml follows):

main_screen

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >

   <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:text="@string/welcome"
        android:gravity="center"
        android:layout_weight="3" />

   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:layout_weight="2" > 

       <Button
           android:id="@+id/resume_button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/resume" />

       <Button
           android:id="@+id/newgame_button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/new_game" />

       <Button
           android:id="@+id/quit_button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="@string/quit" />
    </LinearLayout>

</LinearLayout>

I got the layout that I wanted, but did I go about it in a good way? Is there a better way to get this result?

Secondly, say I wanted to transform my original screen to this: main_transformed_1

Is a good way to do this with a combination of alignment via gravity (LEFT) and padding?

Thanks in advance.

Upvotes: 1

Views: 34

Answers (1)

Neoh
Neoh

Reputation: 16164

You can remove the redundant LinearLayout that encloses those buttons

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:layout_weight="2" > 

   <Button
       android:id="@+id/resume_button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/resume" />

   <Button
       android:id="@+id/newgame_button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/new_game" />

   <Button
       android:id="@+id/quit_button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/quit" />
</LinearLayout>

and set each button with android:layout_weight="1". You can also set parent linearlayout to android:layout_weight="4" to get back the about the same weight.

Upvotes: 1

Related Questions