Android:将按钮放在文本下面,而不是同一行

时间:2021-10-04 14:02:45

I am making a simple Android Application.

我正在做一个简单的Android应用。

This is the XML code:

这是XML代码:

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

which is giving the output: http://flic.kr/p/dqwmT9

输出是:http://flic.kr/p/dqwmT9

But, I want the Buttons to be present below the "Score: 0"

但是,我希望按钮显示在“Score: 0”以下

I have tried a few things, which are giving the output: http://flic.kr/p/dqwbxD

我尝试了一些东西,它们给出了输出:http://flic.kr/p/dqwbxD

What can I do to fix this?

我该怎么做才能解决这个问题呢?

NOTE: I have given the link because I do not have enough reputation to upload photos.

注意:我已经给出了链接,因为我没有足够的声誉来上传照片。

4 个解决方案

#1


1  

Try this. You can, in fact, just copy and paste it. Change the attributes and you are good to go.

试试这个。实际上,你可以复制粘贴。改变属性,你就可以开始了。

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

#2


0  

You want to use a RelativeLayout Then assign an id to the TextView that shows "Score":

你想要使用一个RelativeLayout然后给显示“Score”的TextView分配一个id:

<TextView
    android:id="@+id/score_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Score: "
    tools:context=".Adder" />

Then you have assign position to the Buttons based on that:

然后你可以根据这些按钮分配位置:

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

#3


0  

Just use the Relative Layout instead of Linear Layout.

只使用相对布局而不是线性布局。

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

#4


-1  

You have to set

你必须设置

android:orientation="vertical"

to the parent layout (if it is a LinearLayout). If your parent Layout is a RealativeLayoutyou have to set

到父布局(如果是线性布局)。如果你的父布局是真实的,你必须设置

android:below="id of the view above"

to the buttons you want to have below the TextView.

到TextView下面的按钮。

Edit(Since your ParentLayout is a LinearLayout):

编辑(因为你的父布局是线性布局):

 <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
    >

    <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" />
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
    >

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

/>

#1


1  

Try this. You can, in fact, just copy and paste it. Change the attributes and you are good to go.

试试这个。实际上,你可以复制粘贴。改变属性,你就可以开始了。

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

#2


0  

You want to use a RelativeLayout Then assign an id to the TextView that shows "Score":

你想要使用一个RelativeLayout然后给显示“Score”的TextView分配一个id:

<TextView
    android:id="@+id/score_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Score: "
    tools:context=".Adder" />

Then you have assign position to the Buttons based on that:

然后你可以根据这些按钮分配位置:

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

#3


0  

Just use the Relative Layout instead of Linear Layout.

只使用相对布局而不是线性布局。

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

#4


-1  

You have to set

你必须设置

android:orientation="vertical"

to the parent layout (if it is a LinearLayout). If your parent Layout is a RealativeLayoutyou have to set

到父布局(如果是线性布局)。如果你的父布局是真实的,你必须设置

android:below="id of the view above"

to the buttons you want to have below the TextView.

到TextView下面的按钮。

Edit(Since your ParentLayout is a LinearLayout):

编辑(因为你的父布局是线性布局):

 <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
    >

    <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" />
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
    >

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

/>