[英]How to Align a trailing string to the bottom right corner of a TextView
In the picture bellow i want the Footnote
string to always appear floating to the right edge of the Textview:在下面的图片中,我希望
Footnote
字符串始终浮动到 Textview 的右边缘:
I tried the following but it doesn't work:我尝试了以下但它不起作用:
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);
It can be achieved by using 2 textviews in frame layout and set the value of layout_gravity as right and bottom of 2nd second textview. 可以通过在框架布局中使用2个文本视图并将layout_gravity的值设置为第二个第二文本视图的右侧和底部来实现。
android:layout_gravity="bottom|right"
android:layout_gravity =“ bottom | right”
Example: 例:
<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>
In case of single line, second highlighted word overlaps the text. 如果是单行,则第二个突出显示的单词会与文本重叠。 It can be handled programatically:
可以通过编程方式处理:
/**
* 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);
}
}
});
}
This can be achieved by having a negative margin for the Footnote Textview as shown below. 可以通过在脚注文本视图中使用负边距来实现,如下所示。
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.