如何在 Android 的布局中定位 TextView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20192925/
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 02:26:43  来源:igfitidea点击:

How to position a TextView in a layout in Android?

androidandroid-layouttextview

提问by KousiK

I want to design this kind of layout, which will have a title and a background image as shown in the below image, and three TextViews at the center of each separator. When one of the TextViews text size increases, the other TextViews will remain at the same position.

我想设计这种布局,它将有一个标题和一个背景图像,如下图所示TextView,每个分隔符的中心有三个s。当TextViews 文本大小之一增加时,其他TextViews 将保持在同一位置。

enter image description here

在此处输入图片说明

How can I achieve this?

我怎样才能做到这一点?

回答by Kaushik

You can try the following code:

您可以尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="TextView" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>

回答by Hariharan

Try this..

尝试这个..

<?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"
    android:background="#0D181E" >


    <TextView
        android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:layout_margin="5dp"
            android:textSize="20dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="true"
        android:layout_below="@+id/txt"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:layout_margin="5dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:textSize="20dp" />

        <view 
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:textColor="#FFFFFF"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:layout_margin="5dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:textSize="20dp" />
         <view 
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:textColor="#FFFFFF"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:layout_margin="5dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:textSize="20dp" />
        </LinearLayout>
</RelativeLayout>

回答by user2944178

U can drag and drop in xml file has your wish but advise is use relative layout. it may be use full some scenarios

您可以根据需要在 xml 文件中拖放,但建议使用相对布局。它可能会充分利用某些场景