【问题标题】:Why do RelativeLayout buttons override each other?为什么 RelativeLayout 按钮会相互覆盖?
【发布时间】:2012-10-02 21:05:13
【问题描述】:

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

<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>

【问题讨论】:

    标签: android android-layout overriding android-button android-relativelayout


    【解决方案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: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>
    

    【讨论】:

      【解决方案2】:

      试试这个

           <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>
      

      【讨论】:

        【解决方案3】:
        <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>
        

        【讨论】:

          【解决方案4】:

          在创建任何 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>
          

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-07-02
            • 2017-08-02
            • 1970-01-01
            • 1970-01-01
            • 2021-03-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多