Android - 有更好的方法来进行此布局吗?

时间:2010-12-06 22:18:03

标签: android-layout

我对android很新,并且想知道是否有更好的方法来做这个布局。我需要在左侧垂直堆叠4个标签,右侧有2个较大的字体标签。如果你在模拟器中运行它,它正是我需要它的方式,但是有更优选的方式吗?

<?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"
        android: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>

3 个答案:

答案 0 :(得分:0)

如果您开始部署到具有不同屏幕尺寸/密度的不同设备上,您可能会看到一些东西。

如果这是我的问题,我很想使用额外的LinearLayout并使用加权对齐它们。见下文..

希望这对你有所帮助,如果还不晚:)

<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>

答案 1 :(得分:0)

马克,我认为这不是一个好方法,因为Android文档指的是相对效率更高。你可以在这里找到它: http://developer.android.com/guide/topics/ui/layout/relative.html

  

“RelativeLayout是一个非常强大的设计用户实用程序   接口,因为它可以消除嵌套视图组并保持您的   布局层次结构平坦,提高了性能。“

答案 2 :(得分:0)

它也可以通过使用TableLayout来完成。因此,您可以连续添加标签(TableRow)。希望它对你有用: - )

相关问题