Android 布局下方和上方

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21574996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:45:55  来源:igfitidea点击:

Android Layout Below and Above

androidxmlandroid-layoutlayout

提问by thankyou

I got three TextViews. The first and the third are defined with a not changing Text. The second, the TextView between the first and the third gets the value with an input. How can I set the second TextView between the first and the third in the layout?

我得到了三个 TextView。第一个和第三个是用不改变的文本定义的。第二个,第一个和第三个之间的 TextView 通过输入获取值。如何在布局中的第一个和第三个之间设置第二个 TextView?

<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/firsttext"
android:layout_above="@+id/thirdtext"
android:layout_alignLeft="@+id/firsttext"
android:layout_alignRight="@+id/thirdtext"
android:background="@drawable/border"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />

That doesn't work!

那不行!

<TextView
        android:id="@+id/secondtext"
        android:layout_width="282dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/firsttext"
        android:layout_below="@+id/firsttext"
        android:background="@drawable/border"
        android:ellipsize="marquee"
        android:lines="4"
        android:scrollbars="vertical"
        android:textAppearance="?android:attr/textAppearanceMedium" />

That also doesn't work

这也行不通

采纳答案by Pararth

Too many dependencies, check if you are getting any circular dependency error in xml. As per your requirement, without specifying the spacing and other UI details, a solution:

依赖太多,检查xml中是否有循环依赖错误。根据您的要求,无需指定间距和其他 UI 细节,解决方案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black" >

    <TextView
        android:id="@+id/firsttext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:text="first"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/secondtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/firsttext"
        android:background="@android:color/white"
        android:text="second"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/thirdtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/secondtext"
        android:background="@android:color/white"
        android:text="third"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

回答by Andreas Constantinou

Define a LinearLayoutwith height="wrap_content"and width="fill_parent".

LinearLayoutheight="wrap_content"和定义一个width="fill_parent"

You can overcome your problem with two ways. Either add android:layout_below="@id/TextViewExample"in your Textviewsor use android:layout_marginTop="10dip"

您可以通过两种方式克服您的问题。添加android:layout_below="@id/TextViewExample"在您的Textviews或使用android:layout_marginTop="10dip"

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <TextView
            android:id="@+id/firsttext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:text="first"
             android:layout_marginTop="10dip"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/secondtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/firsttext"
            android:background="@android:color/white"
            android:text="second"
            android:layout_below="@id/firstText"
             android:layout_marginTop="20dip"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/thirdtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/secondtext"
            android:background="@android:color/white"
            android:text="third"
             android:layout_marginTop="30dip"
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:layout_below="@id/secondtext"/>

    </LinearLayout>

回答by ElDuderino

Try

尝试

android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_toLeftOf="@id/thirdText"
android:layout_toRightOf="@id/firstText"

Make sure to add your left and right TextView first with the fixed width and

确保首先使用固定宽度添加左右 TextView 和

android:layout_alignParentLeft="true"

and

android:layout_alignParentRight="true" 

回答by saa

Your question is not clear. But try this.. If you are using Relative layout as parent then

你的问题不清楚。但是试试这个..如果你使用相对布局作为父级那么

<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/firsttext"
android:background="@drawable/border"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />

to show three textviews in horizontal way.

以水平方式显示三个文本视图。

<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firsttext"
android:background="@drawable/border"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />

to show in vertical way..

以垂直方式显示..