1个居中按钮,如何在居中按钮上方放置10dp的另一个按钮? Android,IntelliJ

时间:2013-10-23 09:15:16

标签: android xml relativelayout

我有两个按钮的视图。一个(button2)我把android:layout_centerInParent =“true”,它在屏幕的中心位置应该是。现在我希望另一个(button1)放在按钮2上方,边距为10dp。我正在使用相对布局。我怎么能用xml写这个?我应该使用其他布局来使其工作吗?

我的.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bakgrundsbild"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <Button
            android:id="@+id/button1"
            android:layout_width="280dp"
            android:layout_height="50dp"
            android:background="@drawable/nybutton"
            android:text="@string/las_sollefteabladet"
            android:layout_centerHorizontal="true"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="280dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/nybutton"
        android:text="@string/annonsorer"
        android:layout_centerInParent="true"/>



</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

enter image description here像这样使用你的布局..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<Button
    android:id="@+id/button2"
    android:layout_width="280dp"
    android:layout_height="50dp"
    android:layout_centerInParent="true"
    android:layout_marginTop="10dp"
    android:text="annonsorer" />

<Button
    android:id="@+id/button1"
    android:layout_width="280dp"
    android:layout_height="50dp"
    android:layout_above="@+id/button2"
    android:text="las_sollefteabladet" /></RelativeLayout>

将图像添加到..

答案 1 :(得分:0)

机器人:layout_above = “@ + ID / BUTTON2”

在Button1(居中位置)代码

中使用它
相关问题