1

这是我正在处理的一个玩具项目的当前主屏幕(.xml 如下):

主屏幕

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

我得到了我想要的布局,但我做得很好吗?有没有更好的方法来获得这个结果?

其次,假设我想将原始屏幕转换为: main_transformed_1

结合通过重力(LEFT)和填充对齐是一种好方法吗?

提前致谢。

4

1 回答 1

1

您可以删除LinearLayout包含这些按钮的冗余

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

并设置每个按钮android:layout_weight="1"。您还可以设置父线性布局以android:layout_weight="4"获取大约相同的权重。

于 2013-06-09T17:55:04.497 回答