在androidxml中使用相对布局
我正试图用XML创建一个布局,但我对如何以这种格式定位按钮有点困惑。我已经创建了按钮,并尝试了不同的方法,如alignParentBottom、alignParentRight等,但我似乎无法按我希望的方式获得它。有人能帮我吗在androidxml中使用相对布局,android,xml,button,android-relativelayout,Android,Xml,Button,Android Relativelayout,我正试图用XML创建一个布局,但我对如何以这种格式定位按钮有点困惑。我已经创建了按钮,并尝试了不同的方法,如alignParentBottom、alignParentRight等,但我似乎无法按我希望的方式获得它。有人能帮我吗 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:lay
<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" />
好的,在RelativeLayout中,顾名思义,元素是相对于其他东西定位的,比如父视图或布局中的另一个小部件。因此,您可以通过多种方式对齐小部件,例如将其定位到视图的右侧、左侧、上方、下方等 有关更多信息,请参阅和 因此,如果要使按钮看起来与示例图像完全相同,请尝试以下操作:
<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" />
您可以简单地将按钮宽度设置为填充父项,而不是左右对齐。和高度,您可以定义要使用的度量
顺便说一句,我使用了alignParentLeft和alignParentStart,因为我不知道您使用的是哪种API级别,所以我使用了新旧两个参数。但效果是一样的,您的按钮将始终从父视图的左侧开始
编辑:根据用户hasternet的建议,您也可以使用LinearLayout并获得类似的结果
编辑2:如果您想更改按钮的外观,请尝试更改样式(了解我的意思)。好的,在RelativeLayout中,正如名称所示,元素的位置是相对于其他对象的,如父视图或布局中的另一个小部件。因此,您可以通过多种方式对齐小部件,例如将其定位到视图的右侧、左侧、上方、下方等 有关更多信息,请参阅和 因此,如果要使按钮看起来与示例图像完全相同,请尝试以下操作:
<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" />
您可以简单地将按钮宽度设置为填充父项,而不是左右对齐。和高度,您可以定义要使用的度量
顺便说一句,我使用了alignParentLeft和alignParentStart,因为我不知道您使用的是哪种API级别,所以我使用了新旧两个参数。但效果是一样的,您的按钮将始终从父视图的左侧开始
编辑:根据用户hasternet的建议,您也可以使用LinearLayout并获得类似的结果
编辑2:如果您想更改按钮的外观,请尝试更改样式(了解我的意思)。您的代码似乎正确,但如果您想复制按钮的大小,则无法将高度设置为
包裹内容。这将使其与文本一样高,将其更改为某个给定值:
android:layout_height="50dp"
您的代码似乎是正确的,但如果要复制按钮的大小,则不能将高度设置为wrap\u content
。这将使其与文本一样高,将其更改为某个给定值:
android:layout_height="50dp"
由于您需要以不同的方式设置按钮的高度,并且它取决于不同的屏幕大小,因此相对布局无法实现这一点
所以选择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>
由于您需要以不同的方式设置按钮的高度,并且取决于不同的屏幕大小,因此相对布局无法实现这一点
所以选择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>
我们可以通过权重和在所有屏幕和所有分辨率下对其进行管理
在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>
我们可以在所有屏幕和所有分辨率下按权重和进行管理
在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>
将其粘贴到XML中
将其粘贴到XML中
尝试将布局从“相对”更改为“线性”,并使用垂直方向。然后按您希望显示的顺序添加按钮(第一个1、2、3和4),尝试将布局从相对更改为线性,垂直方向。然后按您希望显示的顺序添加按钮(前1、2、3和4),感谢它的工作。你忘了写这行,xmlns:android=”“。除此之外,再次感谢!我还有一个问题,如果你能帮我,那就太好了。如何用xml实现这种布局?再次感谢。在您的xml中粘贴下面的代码,这是我最后为您的第二个问题添加的代码,并投票支持我的解决方案。您好,投票支持我需要15次代表,我已将您的答案标记为正确,当我有15次代表时,我将投票支持您的两个答案@Android。。。。谢谢你的欣赏,以后也可以随意问任何问题。谢谢你的帮助。你忘了写这行,xmlns:android=”“。除此之外,再次感谢!我还有一个问题,如果你能帮我,那就太好了。如何用xml实现这种布局?再次感谢。在您的xml中粘贴下面的代码,这是我最后为您的第二个问题添加的代码,并投票支持我的解决方案。您好,投票支持我需要15次代表,我已将您的答案标记为正确,当我有15次代表时,我将投票支持您的两个答案@Android。。。。谢谢你的欣赏,以后也可以随意提问。