Android:将按钮放在文本下方,而不是同一行
我正在制作一个简单的Android应用程序 这是XML代码:Android:将按钮放在文本下方,而不是同一行,android,xml,android-layout,Android,Xml,Android Layout,我正在制作一个简单的Android应用程序 这是XML代码: <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Score: " tools:cont
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Score: "
tools:context=".Adder" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
tools:context=".Adder" />
<Button
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="1"
android:textSize="20dp" />
<Button
android:id="@+id/two"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="2"
android:textSize="20dp" />
它给出了输出:
但是,我希望按钮出现在“分数:0”下面
我已经尝试了一些方法,这些方法可以提供输出:
我能做些什么来解决这个问题
注意:我提供了链接,因为我没有足够的声誉上传照片。您必须设置
android:orientation="vertical"
android:below="id of the view above"
到父布局(如果是线性布局
)。
如果父布局是RealativeLayout
,则必须设置
android:orientation="vertical"
android:below="id of the view above"
添加到TextView
下面您想要的按钮
编辑(因为父布局是一个线性布局
):
/>
/>
试试这个。事实上,你可以复制粘贴它。更改属性,您就可以开始了
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/txtScoreLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="Score" />
<TextView
android:id="@+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtScoreLabel"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/txtScoreLabel"
android:text="0" />
<Button
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtScoreLabel"
android:layout_below="@+id/txtScoreLabel"
android:text="Button One" />
<Button
android:id="@+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/one"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/one"
android:text="Button Two" />
</RelativeLayout>
您想使用一个
RelativeLayout
然后为显示“分数”的文本视图分配一个id:
然后,您可以根据以下内容为按钮指定位置:
<Button
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@id/score_text"
android:text="1"
android:textSize="20dp" />
<Button
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@id/score_text"
android:layout_toRightOf="@id/one"
android:text="2"
android:textSize="20dp" />
只需使用相对布局而不是线性布局
<Button
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@id/score_text"
android:text="1"
android:textSize="20dp" />
以线性布局包裹按钮,并将方向设置为水平