简体   繁体   English

无法在两行中创建两个TextView

[英]Cannot create two TextView in two lines

This is rather a layman question as I am totally green in Android development. 这是一个门外汉的问题,因为我在Android开发中完全是绿色的。

Recently I am trying to implement a simple calculator using android. 最近,我正在尝试使用android实现一个简单的计算器。

I want to make two rows of calculator display, with the top one showing what equations the user has input, and the bottom one with the answer. 我要显示两行计算器,最上面的一行显示用户输入的方程式,最下面的一行显示答案。

Here is my code in the layout xml file: 这是我在布局xml文件中的代码:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linearLayout0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true">

    <TextView
        android:id="@+id/calculatorDisplay0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" 
    android:layout_below="@+id/linearLayout0" >

    <TextView
        android:id="@+id/calculatorDisplay1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_below="@id/calculatorDisplay0"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />
</LinearLayout>
     .
     .
     .
     .

However, this is the result: 但是,这是结果:

在此处输入图片说明

As you can see, the two lines are nested together. 如您所见,这两行嵌套在一起。

I am pretty sure something wrong with my code, but I cannot locate it. 我很确定我的代码有问题,但是我找不到它。 Could anyone help me on it? 有人可以帮我吗?

There's no reason to use separate linear layouts for each of the text views - put them into one layout with vertical orientation, one after another: 没有理由为每个文本视图使用单独的线性布局-将它们以垂直方向放置在一个布局中,一个接一个地放置:

<LinearLayout
    android:id="@+id/linearLayout0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/calculatorDisplay0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/calculatorDisplay1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />
</LinearLayout>

Also, you have some unnecessary attributes, which I removed in y answer. 另外,您还有一些不必要的属性,已在y答案中将其删除。

Instead of adding two LinearLayouts add one with two TextView and set orientation to vertical . 而不是添加两个LinearLayouts,而是添加一个带有两个TextView的对象,并将orientation设置为vertical Linear layout is the container for storing "linear" content, so one child just after another. 线性布局是用于存储“线性”内容的容器,因此一个孩子一个接一个。

<LinearLayout
    android:id="@+id/linearLayout0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/calculatorDisplay0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/calculatorDisplay1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_below="@id/calculatorDisplay0"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />
</LinearLayout>

尝试从linearLayout1删除android:layout_alignParentTop =“ true”并在其顶部添加边距。

android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"

Those are attributes in the second LinearLayout where you have explicitly declared that you want second TextView (content of the LinearLayout) to overlap with what already is at the top. 这些是第二个LinearLayout中的属性,在该属性中您已明确声明要让第二个TextView(LinearLayout的内容)与顶部的重叠。 I'm not sure but I think you would get the same without specifying layout_* attributes in that LinearLayout. 我不确定,但我认为如果layout_*该LinearLayout中指定layout_*属性,您将得到相同的结果。 On the second look it seams android:layout_below="@+id/linearLayout0" attribute was ignored. 在第二个外观上,它接缝android:layout_below="@+id/linearLayout0"属性被忽略了。 In any case, it was in conflict with layout_alignParentTop , watch out for that in the future. 无论如何,它都与layout_alignParentTop冲突,请layout_alignParentTop注意。

The desired behavior you have described is typical for LinearLayout with android:orientation="vertical" attribute. 您描述的期望行为是具有android:orientation="vertical"属性的LinearLayout的典型行为。 You were on the right track. 您在正确的轨道上。 What you have to change is to move second TextView in the first LinearLayout, so it can see that it has two views to manage and add desired orientation attribute because default orientation is horizontal [1] . 您需要更改的是在第一个LinearLayout中移动第二个TextView,因为默认方向是水平[1] ,所以可以看到它有两个视图可以管理和添加所需的orientation属性。

Layout XML: 布局XML:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/linearLayout0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/calculatorDisplay0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />

    <TextView
        android:id="@+id/calculatorDisplay1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_below="@id/calculatorDisplay0"
        android:maxLines="1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="40sp" />

</LinearLayout>
     .
     .
     .
     .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM