如何更有效地设置布局的大小?

时间:2012-08-08 07:10:44

标签: android

我需要将我的视图放在中心,它的宽度不是match parentwrap content,而是应用程序运行的屏幕的特定百分比大小。为了获得更灵活的布局,我没有使用尺寸设置其尺寸,而是为元素定义了具有所需尺寸的权重。为了做到这一点,我为它们插入了两个额外的LinearLayouts和定义的权重。我不认为这是在XML中增加Views数量的最佳解决方案。你能告诉我更有效的方法吗?

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@drawable/right_back"
                  android:orientation="horizontal"
                  android:gravity="center">

<!-- The first one I think which is redundant -->
        <LinearLayout
            android:layout_weight="25"
            android:layout_width="0dip"
            android:layout_height="wrap_content"/>

<!-- Main view I need to be of a required size on each screen size -->
        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:background="@drawable/layout_round_corners"
            android:orientation="vertical"
            android:layout_weight="50"
            android:padding="20dip">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/welcome_text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/text_color"
                android:layout_gravity="center_horizontal"/>

            <EditText
                android:id="@+id/edt_firsttimeactivity_first_field"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="@string/password"
                android:password="true"
                android:singleLine="true"/>

            <EditText
                android:id="@+id/edt_firsttimeactivity_second_field"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/repeat_new_password_again"
                android:password="true"
                android:singleLine="true"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:onClick="onButtonClick"
                android:text="@string/create_account_for_me"/>
        </LinearLayout>

<!-- The second one I think which is redundant -->
        <LinearLayout
            android:layout_weight="25"
            android:layout_width="0dip"
            android:layout_height="wrap_content"></LinearLayout>


    </LinearLayout>

2 个答案:

答案 0 :(得分:0)

我认为问题在于你没有定义weightSum。您应该将android:weightSum="100"(例如)添加到最外面的LinearLayout,然后将所需的总和除以内部布局。

编辑:如果我正确理解了这个问题,我认为您应该只需使用android:paddingLeftandroid:paddingRight作为“主视图”即可解决问题。您还可以尝试android:layout_marginLeftandroid:layout_marginRight。当然,在这种情况下,将高度保留为fill_parent。希望有所帮助。

答案 1 :(得分:0)

这是你应该如何编写布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"                  
              android:orientation="horizontal"
              android:background="@drawable/right_back"
              android:weightSum="1"
              android:gravity="center">

<!-- Main view I need to be of a required size on each screen size -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/layout_round_corners"
        android:layout_weight=".5"
        android:padding="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/welcome_text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/text_color"
            android:layout_gravity="center_horizontal"/>

        <EditText
            android:id="@+id/edt_firsttimeactivity_first_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"                
            android:hint="@string/password"
            android:password="true"
            android:singleLine="true"/>

        <EditText
            android:id="@+id/edt_firsttimeactivity_second_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/repeat_new_password_again"
            android:password="true"
            android:singleLine="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:onClick="onButtonClick"
            android:text="@string/create_account_for_me"/>
    </LinearLayout>
</LinearLayout>

试试这个。你可以改变第二个LinearLayout的layout_weight,使它看起来像你想要的大或小。