-1

我似乎无法使用相对链接来显示按钮。它们粘在一起,就像按钮相互覆盖,我试图让它们不粘在一起,但无济于事。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:background="@drawable/formation"
android:layout_height="fill_parent" >

<Button
    android:layout_weight="1.0"
    android:layout_gravity="bottom"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Laws" />
<Button 
    android:layout_weight="1.0"
    android:layout_gravity="bottom"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Qualifications of a naval officer" />
<Button
    android:layout_weight="1.0" 
    android:layout_gravity="bottom"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Code of Conduct" />
</RelativeLayout>
4

4 回答 4

1

在创建任何 xml 文件时,请记住每个小部件的 id 都是唯一的,因此首先您必须为每个小部件分配 id。

现在,您在这里使用的是相对布局。因此,您在此处添加的每个小部件都将相互关联。

现在看到这段代码,我确实改变了你的代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:background="@drawable/formation"
   android:layout_height="fill_parent" >

<Button
android:id="@+id/btn1"
android:layout_weight="1.0"
android:layout_gravity="bottom"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Laws" />
<Button 
android:id="@+id/btn2"
android:layout_gravity="bottom"
android:layout_below="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Qualifications of a naval officer" />
<Button
android:id="@+id/btn3"
android:layout_gravity="bottom"
android:layout_below="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code of Conduct" />
</RelativeLayout>

所以所有的按钮都会一一显示。

于 2012-10-12T05:20:36.547 回答
0

这将把事情放在另一边。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:background="@drawable/formation"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/button1"
    android:layout_weight="1.0"
    android:layout_gravity="bottom"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Laws" />
<Button 
    android:id="@+id/button2"
    android:layout_toRightOf="@id/button1"
    android:layout_weight="1.0"
    android:layout_gravity="bottom"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Qualifications of a naval officer" />
<Button
    android:id="@+id/button3"
    android:layout_toRightOf="@id/button2"
    android:layout_weight="1.0" 
    android:layout_gravity="bottom"
    android:layout_alignParentTop="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Code of Conduct" />
</RelativeLayout>
于 2012-10-12T05:06:05.203 回答
0

试试这个

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/formation" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="bottom"
                android:text="@string/Laws"  />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="bottom"
                android:layout_toRightOf="@+id/button1"
                android:text="Code of Conduct" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_toRightOf="@+id/button2"
                android:text="Qualifications of a naval officer" />

        </RelativeLayout>
于 2012-10-12T05:09:03.037 回答
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Laws" />

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btn1"
    android:text="Code of Conduct" />

<Button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/btn2"
    android:text="Qualifications of a naval officer" />
</RelativeLayout>
于 2012-10-12T05:16:47.200 回答