【问题标题】:Working with relative layout in android XML在 android XML 中使用相对布局
【发布时间】:2015-02-07 20:45:28
【问题描述】:

我正在尝试按照http://i.stack.imgur.com/aPoeU.png 的方式在 XML 中创建一个布局,但我对如何以这种格式定位按钮有点困惑。我已经创建了按钮并尝试了不同的东西,如 alignParentBottom、alignParentRight 等,但我似乎无法按照我想要的方式得到它。有人可以帮帮我吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button2"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button3"
    android:text="Button" />

【问题讨论】:

  • 尝试将布局从相对更改为线性,垂直方向。然后按您希望显示的顺序添加按钮(前 1、2、3 和 4)

标签: android xml button android-relativelayout


【解决方案1】:

嗯,在RelativeLayout 中,顾名思义,元素是相对于其他东西定位的,比如父视图或布局中的另一个小部件。因此,您可以通过多种方式对齐小部件,例如将其附加到视图的右侧、左侧、上方、下方等等。

更多信息请参考this linkthis other link

所以,如果你想让你的按钮看起来和你的示例图片一模一样,试试这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:text="Button 1" />

<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/button1"
    android:text="Button 2" />

<Button
    android:id="@+id/button3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/button2"
    android:text="Button 3" />

<Button
    android:id="@+id/button4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/button3"
    android:text="Button 4" />

您可以简单地将按钮宽度设置为 fill_parent 而不是左右对齐。而高度,你可以定义你想要使用的度量。

顺便说一句,我使用了 alignParentLeft 和 alignParentStart,因为我不知道您使用的是哪个 API 级别,所以我使用了新旧参数。但效果是一样的,你的按钮总是从父视图的左边开始。

编辑:正如用户 hasternet 所建议的,您也可以使用 LinearLayout 并获得类似的结果。

编辑 2:如果您想更改按钮的外观,请尝试更改样式(以 a look here 了解我的意思)。

【讨论】:

    【解决方案2】:

    您的代码似乎正确,但如果您想复制按钮的大小,则不能将高度设置为wrap_content。这将使其与文本一样高,将其更改为某个给定值:

    android:layout_height="50dp"
    

    【讨论】:

      【解决方案3】:

      由于您需要设置不同的按钮高度,并且取决于不同的屏幕尺寸,因此您无法使用相对布局来实现这一点。

      所以选择 LinearLayout 和 weightsum 属性。通过这种方式屏幕大小,您可以实现您预期的 UI。下面提到的代码已修改。

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity"
          android:Orientation="vertical"
          android:weightsum="8" >
      
          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:layout_alignParentTop="true"
              android:text="Button" 
              android:layout_weight="1"
      />
      
          <Button
              android:id="@+id/button2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:layout_below="@+id/button1"
              android:text="Button"
              android:layout_weight="3"
       />
      
          <Button
              android:id="@+id/button3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:layout_below="@+id/button2"
              android:text="Button" 
              android:layout_weight="3"/>
      
          <Button
              android:id="@+id/button4"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentRight="true"
              android:layout_below="@+id/button3"
              android:text="Button" 
              android:layout_weight="1"/>
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        我们可以通过 weight_sum 在所有屏幕和所有分辨率下管理它

        将以下代码粘贴到您的 xml 中

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
             android:background="#000"
            android:weightSum="1" >
        
            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
        
             android:background="#bebebe"
                android:layout_weight="0.30"
                android:text="Button1" />
        
            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="2dp"
                android:layout_weight="0.20"
                android:background="#bebebe"
                android:text="Button2" />
        
            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="2dp"
                android:layout_weight="0.20"
                android:background="#bebebe"
                android:text="Button3" />
        
            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="2dp"
                android:layout_weight="0.30"
                android:background="#bebebe"
                android:text="Button4" />
        
        </LinearLayout>
        

        【讨论】:

        • 感谢它的工作。你忘了写这行,xmlns:android="schemas.android.com/apk/res/android"。除此之外,再次感谢!
        • 我还有 1 个问题,如果你能帮助我,那就太好了。如何在 xml 中实现这种布局? postimg.org/image/s5q0ri89z 再次感谢。
        • 将以下代码粘贴到您的 xml 中,我最后为您的第二个问题添加了该代码,并为这个解决方案投票。
        • 您好,要投票,我需要 15 个代表,我已将您的答案标记为正确,当我有 15 个代表时,我会为您的两个答案投票
        • 您好@Android....感谢您的赞赏,以后也可以随时提出任何类型的问题。
        【解决方案5】:

        将其粘贴到您的 XML 中

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#2258A2"
        android:gravity="center"
        android:orientation="vertical" >
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000"
            android:orientation="vertical"
            android:weightSum="1" >
        
            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.40"
                android:background="#bebebe"
                android:text="Button1" />
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="1dp"
                android:layout_weight="0.20"
                android:orientation="horizontal"
                android:weightSum="1" >
        
                <Button
                    android:id="@+id/button2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginRight="1dp"
                    android:layout_weight="0.50"
                    android:background="#bebebe"
                    android:text="Button2" />
        
                <Button
                    android:id="@+id/button3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.50"
                    android:background="#bebebe"
                    android:text="Button3" />
            </LinearLayout>
        
            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="1dp"
                android:layout_weight="0.40"
                android:background="#bebebe"
                android:text="Button4" />
        </LinearLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多