【问题标题】:TextViews alignment with dynamic contentTextViews 与动态内容对齐
【发布时间】:2017-01-11 16:12:11
【问题描述】:

当前视图:

这是预期的结果:

问题是红色文本是动态的,直到运行时我才知道长度。

目前我有一个LinearLayout,其中有两个TextViews,一个用于红色文本,另一个用于可点击链接。我设法用LinearLayout android:orientation=vertical 显示红色文本下方的链接。

如果我将其设置为android:orientation=horizontal,链接会转到最右侧,而不是红色文本内容的旁边。

我当前的 XML:

<LinearLayout
        android:paddingLeft="16dp"
        android:paddingRight="@dimen/card_container_padding"
        android:paddingTop="@dimen/card_container_padding"
        android:paddingBottom="@dimen/card_container_padding"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/cms_message_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="@dimen/body_text_size"
            android:textStyle="bold"
            android:textAlignment="center"
            android:layout_weight="0"
            android:text=""/>

        <TextView
            android:id="@+id/cms_message_modal_link"
            android:layout_marginLeft="@dimen/account_home_line_margin_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/lightBlue"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            android:layout_gravity="end"
            android:textStyle="bold"
            android:textSize="@dimen/body_text_size"
            android:text="More Info"/>

    </LinearLayout>

我应该使用不同的布局还是有可以帮助我的属性?

【问题讨论】:

  • 问题是您使用LinearLayout 并将orientation 设置为vertical,这意味着您的所有元素都将垂直堆叠。您可以使用RelativeLayout 将第二个TextView 设置在第一个的旁边。

标签: android xml android-layout textview android-linearlayout


【解决方案1】:

虽然 Xan 的回答出于显示目的没有错,但它不允许仅单击下划线部分。我建议使用:

TextView textView = (TextView) findViewById(R.id.cms_message_content);
SpannableString ss = new SpannableString("Here is some non linked text, but this is linked");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View view) {
        Toast.makeText(MainActivity.this, "Hey you clicked me", Toast.LENGTH_SHORT).show();
    }
};
int indexOfComma = ss.toString().indexOf(","); /* just for this case, im coloring after the comma */
ss.setSpan(clickableSpan, indexOfComma + 1, ss.toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinkTextColor(Color.BLUE);

【讨论】:

    【解决方案2】:

    使用 Spannable 仅使用 1 个 TextView 和 setText

    https://developer.android.com/reference/android/text/Spannable.html

    Spannable text = new SpannableString("This is underline and bold text.");
    text.setSpan(new UnderlineSpan(), 8, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    text.setSpan(new StyleSpan(Typeface.BOLD), 22, 26, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    TextView textView = (TextView) findViewById(R.id.textview);
    textView.setText(text);
    

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-05
      • 2013-04-12
      • 2021-03-10
      相关资源
      最近更新 更多