如何将尾随字符串与TextView的右下角对齐

时间:2015-11-11 21:02:18

标签: android textview spannablestring

在下面的图片中,我希望Footnote字符串始终浮动到Textview的右边缘:

enter image description here

我尝试了以下但是它不起作用:

SpannableStringBuilder builder = new SpannableStringBuilder();
SpannableString item = new SpannableString ("Main text");
builder.append (item);
builder.append ("  ");
String footnote = "Footnote";
int len = footnote.length();
SpannableString subitem = new SpannableString (footnote);
subitem.setSpan (new ForegroundColorSpan (color), 0, len, 0);
subitem.setSpan (new AbsoluteSizeSpan (12, true), 0, len, 0);
subitem.setSpan (new AlignmentSpan.Standard (Layout.Alignment.ALIGN_OPPOSITE), 0, len, 0);
builder.append (subitem);

myTextView.setText (builder, TextView.BufferType.SPANNABLE);

2 个答案:

答案 0 :(得分:0)

可以通过在框架布局中使用2个textviews并将layout_gravity的值设置为第二个第二个textview的右边和底部来实现。

  

机器人:layout_gravity ="底部|右"

示例:

<FrameLayout
        android:id="@+id/flString"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text "
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/tvHighlighted"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:text="Footnote"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#0000ff" />
</FrameLayout>

如果是单行,则第二个突出显示的单词与文本重叠。它可以以编程方式处理:

/**
 * Used to create 2 text view text in single line like :
 *
 * Text Text Text Text Text Text Text Text Text Text    |Footnote|
 *
 * @param frameLayout         FrameLayout
 * @param tvTitle             TextView   
 * @param tvHighlighted       TextView
 *       Directly "View" can be used as param type, but for this case only I                     
 *         have used dedicated Views and ViewGroup.
 */
public static void manageTextViews(final FrameLayout frameLayout, final TextView tvTitle, final TextView tvHighlighted) {
    tvTitle.post(new Runnable() {
        @Override
        public void run() {
            try {
                int lineCount = tvTitle.getLineCount();
                Rect bounds = new Rect();
                tvTitle.getLineBounds(lineCount > 0 ? lineCount - 1 : 0, bounds);
                FrameLayout.LayoutParams fllp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                fllp.gravity = (Gravity.BOTTOM | Gravity.RIGHT);
                if (frameLayout.getWidth() <= (bounds.width() + tvBuyable.getWidth() + 10)) {
                    fllp.setMargins(0, tvTitle.getLineHeight() + 5, 0, 0);
                } else {
                    fllp.setMargins(0, 0, 0, 0);
                }
                tvHighlighted.setLayoutParams(fllp);
            } catch (Exception e) {
               Log.e(ClassName + ".manageTextViews()", "mTxtTitle.post", e);
            }
        }
    });
}

答案 1 :(得分:-3)

这可以通过为脚注文本视图设置负余量来实现,如下所示。

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text "
    android:id="@+id/textView"
    android:layout_marginRight="20dp"
    android:textColor="@android:color/black"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Footnote"
    android:layout_below="@id/textView"
    android:layout_alignParentRight="true"
    android:layout_marginTop="-20dp"
    android:layout_marginRight="4dp"
    android:textColor="@color/orange_900"/>

</RelativeLayout>