Android - 在XML布局中构建视图表的最佳实践

时间:2014-08-26 09:17:29

标签: android android-layout android-linearlayout android-xml android-tablelayout

我正在尝试构建一个与屏幕高度和宽度相匹配的视图表。 视图之间的边距应随着屏幕的增长而增长。 我尝试使用重量来做,但是重量控制宽度或高度,而不是同时控制两者。嵌套权重对性能不利。 这就是我试图或多或少地实现的目标 -

enter image description here

这是我迄今为止所尝试过的:

<RelativeLayout 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:background="@drawable/background"
tools:context=".SomeActivity" >

<View
  android:id="@+id/content"
  .../>

<LinearLayout
    android:id="@+id/table"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/content2"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/content"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/firstRow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/secondRow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/thirdRow"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            android:background="@drawable/memory_card_back"
            android:gravity="center"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>
<View
 android:id="@+id/content2" />

我在垂直LinearLayout上使用了3个水平LinearLayouts。 3个水平LaynearLayouts中的每一个都有layout_weight。

如果我忘记提及某事,请告诉我。 感谢。

0 个答案:

没有答案
相关问题