Android - Is there a better way to do this layout?
I'm relatively new to android, and wondering if there's a better way to do this layout. I need 4 labels stacked vertically on the left side, with 2 larger-font labels on the right side. If you run this in the emulator it's exactly how I need it, but is there a more preferred way?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/Label1"
android:text="Label 1: "
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Label2"
android:text="Label 2: "
android:layout_below="@id/Label1"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Label3"
android:text="Label 3: "
android:layout_below="@id/Label2"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Label4"
android:text="Label 4: "
android:layout_below="@id/Label3"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
androi开发者_StackOverflow中文版d:orientation="vertical"
android:layout_toRightOf="@id/Label1">
<TextView
android:id="@+id/Label6"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Points"
android:textStyle="bold"
android:textSize="25sp"
android:gravity="center_horizontal"/>
<TextView
android:id="@+id/LabelPoints"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="8.6"
android:textStyle="bold"
android:textSize="25sp"
android:gravity="center_horizontal"/>
</LinearLayout>
</RelativeLayout>
By the looks of things you may come a little unstuck if you start to deploy onto different devices with different screen sizes / densities.
If this were my problem, I'd be tempted to use an extra LinearLayout and align these using a weighting. See below..
Hope this helps you, if it's not too late :)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/Label1"
android:text="Label 1: "
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Label2"
android:text="Label 2: "
android:layout_below="@id/Label1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Label3"
android:text="Label 3: "
android:layout_below="@id/Label2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Label4"
android:text="Label 4: "
android:layout_below="@id/Label3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:layout_toRightOf="@id/Label1">
<TextView
android:id="@+id/Label6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Points"
android:textStyle="bold"
android:textSize="25sp"
android:gravity="center_horizontal"/>
<TextView
android:id="@+id/LabelPoints"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="8.6"
android:textStyle="bold"
android:textSize="25sp"
android:gravity="center_horizontal"/>
</LinearLayout></LinearLayout>
Mark, I think it is not a good way because Android docs refers that relative is more efficient. You can find it here: http://developer.android.com/guide/topics/ui/layout/relative.html
"A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance."
it can do by using TableLayout,too. so, you can add your labels in a row (TableRow). hopefully, it useful for you :-)
精彩评论