在Android应用布局上,我如何定位视图,使其始终显示在中间下方一半的距离?

时间:2012-02-08 18:56:31

标签: android layout mobile

我使用Relative布局作为根容器或父容器,我有两个按钮放在此布局中。按钮需要一个放在另一个上面。问题是我想要定位这些按钮,使它们出现在视图中心下方但不在下面。这是在视图的下半部分我希望按钮出现在该半部分的中间位置。我尝试在父RelativeLayout中添加一个按钮作为RelativeLayout(在中间居中)的子项,这种类型实现了我正在尝试的但是然后Eclipse抱怨警告声明一组相对布局标签是没用的,我应该考虑摆脱它。

然后我尝试给其中一个按钮提供相对于其父级的上边距,然后将另一个按钮放在带有上边距的此按钮下。这似乎有效,直到我在其他虚拟设备中尝试,我发现根据屏幕尺寸和尺寸,它可能会或可能不会出现在我想要的地方(特别是平板设备的情况)。

好的,那么我不确定如何实现我想要的正确方式(没有警告或错误)。这是我目前的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/default_real" >

    <Button
        android:id="@+id/sm_panel_email_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="300sp"
        android:background="@drawable/info_view_email_button" />


    <Button
        android:id="@+id/sm_panel_web_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/sm_panel_email_button"
        android:background="@drawable/info_view_web_button" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

如何使用两个布局和android:layout_weight属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/default_real"
    android:orientation="vertical" >

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/ >    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
        <Button
            android:id="@+id/sm_panel_email_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/info_view_email_button" />
        <Button
            android:id="@+id/sm_panel_web_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/sm_panel_email_button"
            android:layout_centerHorizontal="true"
            android:background="@drawable/info_view_web_button" />
    </RelativeLayout>

</LinearLayout>