1

我正在使用 Absolute Layout 将屏幕划分为 4 个物种,中心为圆形,如下所示 在此处输入图像描述

它适用于小屏幕尺寸,但不适用于大屏幕。如何用不同的屏幕宽度做到这一点?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<AbsoluteLayout
    android:id="@+id/AbsoluteLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:l="true"
    android:layout_centerInParent="true"
    android:background="#fcf2cf"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.organizer2.MainActivity" >

    <Button
        android:id="@+id/Button1"
        android:layout_width="148dp"
        android:layout_height="180dp"
        android:layout_weight="1"
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:background="#219baa"
        android:text="Button" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="148dp"
        android:layout_height="180dp"
        android:layout_weight="1"
        android:layout_x="148dp"
        android:layout_y="0dp"
        android:background="#ef820b"
        android:text="Button" />

    <Button
        android:id="@+id/Button3"
        android:layout_width="148dp"
        android:layout_height="180dp"
        android:layout_weight="1"
        android:layout_x="0dp"
        android:layout_y="180dp"
        android:background="#e3c800"
        android:text="Button" />

    <Button
        android:id="@+id/Button4"
        android:layout_width="148dp"
        android:layout_height="180dp"
        android:layout_weight="1"
        android:layout_x="148dp"
        android:layout_y="180dp"
        android:background="#36bc89"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_x="4dp"
        android:layout_y="132dp"
        android:src="@drawable/circle" />

   </AbsoluteLayout>

</RelativeLayout>
4

1 回答 1

1

AbsolueLayouts 是一个很大的禁忌,因为它们不会对不同尺寸的设备做任何事情。尝试使用LinearLayouts的组合来实现此视图。通过将所有按钮设置在线性布局内部并为每个按钮赋予 1 的权重,我们告诉它们全部增长,这不会发生在绝对布局内部。线性布局本身也在线性布局中,并且也被赋予了权重,因此它们会随着需要占用的额外屏幕空间而增长(嵌套的线性布局对性能并不好,但现在不要担心,因为你似乎仍然正在学习基础知识,您的应用程序的用户不会注意到任何延迟)。:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear"
android:layout_width="match_parent"
android:background="#fcf2cf"
android:layout_height="match_parent"
android:orientation="vertical" >

    <LinearLayout 
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:tools="http://schemas.android.com/tools"
           android:id="@+id/linear"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight=1
           android:orientation="hoizontal" >

                 <Button
                 android:id="@+id/Button1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:background="#219baa"
                 android:text="Button" />

                 <Button
                 android:id="@+id/Button2"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:background="#ef820b"
                 android:text="Button" />

           </LinearLayout>

           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:tools="http://schemas.android.com/tools"
           android:id="@+id/linear"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight=1
           android:orientation="hoizontal" >

                <Button
                android:id="@+id/Button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#e3c800"
                android:text="Button" />

                <Button
                android:id="@+id/Button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#36bc89"
                android:text="Button" />

          </LinearLayout>
</LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/circle" />

   </AbsoluteLayout>

</RelativeLayout>
于 2014-10-31T20:51:09.773 回答