Android TextView 将文本左右对齐

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

Android TextView Align text to Right and Left

android

提问by c12

I'm trying to have a TextView that has two pieces of text, one aligned against the far left and one against the far right. I can put spaces between the two words, but I was wondering if there is a better technique?

我正在尝试拥有一个包含两段文本的 TextView,一段与最左侧对齐,另一段与最右侧对齐。我可以在两个词之间加空格,但我想知道是否有更好的技巧?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/background_common"
    android:id="@+id/LinearLayout0123">

   <TextView
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:textColor="@color/Black"
        android:textStyle="bold"
        android:textSize="15dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/selector_social_facebook_twitter"
        android:text="Right Text                                  Left Text" />
</LinearLayout>

回答by Zaid Daghestani

Here's another method:

这是另一种方法:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_common">
    <TextView
        android:text="Left Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"/>
    <TextView
        android:text="Right Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"/>
</FrameLayout>

Layout_gravity tells the parent where to put the child. In a FrameLayout, children can go anywhere and are unobstructed by other children. If there is overlap, the last child added is on top. Good luck!

Layout_gravity 告诉父母把孩子放在哪里。在 FrameLayout 中,孩子可以去任何地方并且不受其他孩子的阻碍。如果有重叠,则添加的最后一个子项位于顶部。祝你好运!

回答by dymmeh

Why not have two textviews instead of jamming the text into a single textView

为什么不用两个 textview 而不是将文本塞进一个 textView

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="300dip"
    android:layout_height="fill_parent" 
    android:background="@drawable/background_common"
    android:id="@+id/LinearLayout0123">

   <TextView
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="40dp"
        android:textColor="@color/Black"
        android:textStyle="bold"
        android:textSize="15dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/selector_social_facebook_twitter"
        android:text="Left Text" />

   <TextView
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="40dp"
        android:textColor="@color/Black"
        android:textStyle="bold"
        android:textSize="15dp"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:background="@drawable/selector_social_facebook_twitter"
        android:text="Right Text" />
</LinearLayout>

You can set android:gravity="right" on the right text box if you want the text to be aligned to the right

如果您希望文本向右对齐,您可以在右侧的文本框中设置 android:gravity="right"

回答by daemontus

If all you need is a single line (label: ... value, or something like that), you can actually achieve this using the Alignmentspan. The only problem is, that the span works for the whole paragraph, so you have to put your left/right texts into separate paragraphs and merge them using a "zero-line-height" span into one line.

如果您只需要一行(标签:...值或类似的东西),您实际上可以使用对齐范围来实现这一点。唯一的问题是,跨度适用于整个段落,因此您必须将左/右文本放入单独的段落中,并使用“零行高”跨度将它们合并为一行。

Something like this:

像这样的东西:

public void setLeftRightText(TextView view, String left, String right) {
    SpannableString merged = new SpannableString(left + "\n" + right);
    merged.setSpan(
            new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL),
            0, left.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE
    );
    merged.setSpan(
            new LineOverlapSpan(),
            left.length(), left.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE
    );
    merged.setSpan(
            new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE),
            left.length() + 1, left.length() + 1 + right.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE
    );
    view.setText(merged);
}

Where the LineOverlapSpanis implemented like this:

LineOverlapSpan被这样实现的:

public class LineOverlapSpan implements LineHeightSpan {
    @Override
    public void chooseHeight(final CharSequence text, final int start, final int end, final int spanstartv, final int v, final Paint.FontMetricsInt fm) {
        fm.bottom += fm.top;
        fm.descent += fm.top;
    }
}

See this answerfor more details.

有关更多详细信息,请参阅此答案

回答by Mitul Goti

This will definitely work for you...

这肯定对你有用......

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:text="Left Side"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Right Side"
        android:textSize="15sp"
        android:textStyle="bold" />
</RelativeLayout>

回答by Parthiban Thirumoorthy

This might be better because it doesn't allow the texts to overlap.

这可能会更好,因为它不允许文本重叠。

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

    <TextView
        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="start" />

    <TextView
        android:id="@+id/sub_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1
        android:layout_gravity="end" />
</LinearLayout>

回答by Carson Holzheimer

This is the simplest way I've found to do it. The trick is to use match_parenton the second text view so it takes up the remaining space and allows the gravityattribute to move the text to the end.

这是我找到的最简单的方法。诀窍是match_parent在第二个文本视图上使用,因此它占用剩余空间并允许gravity属性将文本移动到末尾。

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Left Aligned" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="Right Aligned" />
</LinearLayout>