繁体   English   中英

android中的相对布局问题

[英]relative layout problem in android

在我的应用程序中,我使用相对布局进行设计。在一个活动中,屏幕底部有四个按钮。 我的xml代码(横向)不适用于所有设备屏幕。 如图所示,按钮之间有更多空间。 如何设计适合所有分辨率屏幕(横向)的布局。

布局图片为: 在此处输入图片说明

我的xml:

<RelativeLayout android:id="@+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#0000FF"
xmlns:android="http://schemas.android.com/apk/res/android">


<RelativeLayout android:layout_width="fill_parent"
    android:layout_gravity="bottom" android:layout_alignParentBottom="true"
    android:background="@drawable/applicarion_bar" android:layout_height="wrap_content"
    android:id="@+id/relativeLayout4">

        <ImageButton android:id="@+id/homeimageButton2"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/festival_btn" />

        <ImageButton android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/homeimageButton2"
            android:layout_marginLeft="60dp"
            android:layout_width="wrap_content" android:background="@drawable/search_btn"
            android:id="@+id/homeimageButton3"></ImageButton>
        <TextView android:id="@+id/homecalendar4" android:text="MAR 20"
            android:layout_toRightOf="@+id/homeimageButton3"
            android:gravity="center"
            android:textSize="10dp"
            android:layout_marginLeft="60dp"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/calendar_btn" />
        <ImageButton android:id="@+id/homeimageButton5"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dp"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/favorite_btn" />

</RelativeLayout>

将您的relativeLayout4替换为

    <LinearLayout android:id="@+id/put_id_of_ll"
        android:orientation="horizontal" android:background="#012C58"
        android:paddingLeft="4.0dip" android:paddingTop="5.0dip"
        android:paddingRight="4.0dip" android:paddingBottom="1.0dip"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
              <ImageButton android:id="@+id/homeimageButton2"
                android:layout_width="0.0dip"
                android:text="ImageButton"
                android:background="@drawable/festival_btn"
                android:layout_weight="1.0"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_marginBottom="3dip"
                android:layout_marginLeft="2dip" />
            <ImageButton android:id="@+id/homeimageButton3"     
android:background="@drawable/search_btn"
                android:layout_width="0.0dip"
                android:text="btn2"
                android:layout_weight="1.0"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_marginBottom="3dip"
                android:layout_marginLeft="2dip" />
            <TextView android:id="@+id/homecalendar4" android:text="MAR 20"
                android:layout_width="0.0dip"
                android:layout_weight="1.0"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_marginBottom="3dip"
                android:layout_marginLeft="2dip" />
            <ImageButton android:id="@+id/homeimageButton5"
                android:layout_width="0.0dip"
                android:text="btn4"
                android:background="@drawable/favorite_btn"
                android:layout_weight="1.0"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:layout_marginBottom="3dip"
                android:layout_marginLeft="2dip" />
        </LinearLayout>

它会工作。

快乐的编码:)

使用线性布局而不是相对布局,请使用以下代码

<RelativeLayout android:id="@+id/relativeLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="#0000FF"
xmlns:android="http://schemas.android.com/apk/res/android">


<LinearLayout android:layout_width="fill_parent"
    android:layout_gravity="bottom" android:orientation="horizontal"
    android:background="@drawable/applicarion_bar" android:layout_height="wrap_content"
    android:id="@+id/relativeLayout4">

        <ImageButton android:id="@+id/homeimageButton2"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/festival_btn" />

        <ImageButton android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/homeimageButton2"
            android:layout_weight="1"
            android:layout_marginLeft="60dp"
            android:layout_width="wrap_content" android:background="@drawable/search_btn"
            android:id="@+id/homeimageButton3"></ImageButton>

        <TextView android:id="@+id/homecalendar4" android:text="MAR 20"
            android:layout_toRightOf="@+id/homeimageButton3"
            android:gravity="center"
            android:textSize="10dp"
            android:layout_marginLeft="60dp"
            android:layout_weight="1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/calendar_btn" />

        <ImageButton android:id="@+id/homeimageButton5"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:background="@drawable/favorite_btn" />

</LinearLayout>

</RelativeLayout>

尝试使用线性布局,并为所有视图赋予相等的权重。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM