【问题标题】:How to align a TextView to the right of another TextView which breaks to the next line?如何将 TextView 与另一个中断到下一行的 TextView 的右侧对齐?
【发布时间】:2012-07-18 23:03:16
【问题描述】:

我遇到的问题是将第二个TextView 对齐到跨越两行的TextView 的右侧。

我使用的是RelativeLayout,但是当我使用layout_toRightOf 时,它会出现在屏幕顶部,而当我添加layout_alignRight 时,它就会消失。

我很困惑这是如何工作的。您会假设它会跟随第一个 TextView 结束的地方,但事实并非如此。哦,我在宽度和高度上使用wrap_content,以防有人认为这是问题所在。提前致谢。

XML

<TextView 
            android:id="@+id/edit_event_name_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:textStyle="bold"
            android:layout_marginBottom="5dp"
            android:scrollHorizontally="false"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true" />
        <TextView 
            android:id="@+id/show_event_type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"
            android:scrollHorizontally="false"
            android:layout_toRightOf="@+id/edit_event_name_show"
            android:layout_alignBaseline="@+id/edit_event_name_show" />

编辑:对于尝试将事物与 TextViews 很好地对齐时遇到类似问题的任何人,默认情况下没有办法。但是您可以使用SpannableString 并使用SpannableStringBuilder 构建它们。

【问题讨论】:

  • 如果有帮助,请附上您正在使用的代码和屏幕截图。
  • 我做了,我不需要包括我尝试过的每个案例。但是,如果您认为有必要,我将发布 xml。另外,请注意,如果我不包含android:layout_alignBaseline="@+id/edit_event_name_show",它只会出现在屏幕的最顶部。所以这就是我把它放在那里的原因。它适用于我的按钮,
  • “我不需要包括我尝试过的每个案例。”你是对的,我只是想知道你在做什么。通常,代码示例比文字更能表达一个想法。您包含的内容很棒。

标签: android android-relativelayout textview


【解决方案1】:

第三次尝试(成功尝试)

我相信这会对您有所帮助,并且比其他东西容易得多。

尝试使用此reference,但更改字体大小而不是颜色。

Sam 的回答总结了这个链接。 (以防链接在某个时候失效。)

第二次尝试

<?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" >

    <TextView
        android:id="@+id/edit_event_name_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:scrollHorizontally="false"
        android:text=" test 1     jfalksjdflkjasdfj"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/show_event_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edit_event_name_show"
        android:layout_alignParentRight="true"
        android:scrollHorizontally="false"
        android:text="test2          adjfalsjdfla fa sdfasdf asf a"
        android:textSize="10dp" />

</RelativeLayout>

图片:


第一次尝试

为什么不用LinearLayout 包围你的TextViews。这将使它们更有条理,更整洁。

试试这个,让我知道结果如何:

<?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" >

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

        <TextView
            android:id="@+id/edit_event_name_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollHorizontally="false"
            android:text="test1"
            android:textSize="18dp"
            android:textStyle="bold"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/show_event_type"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollHorizontally="false"
            android:text="test2"
            android:textSize="10dp"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

图片:


长文本:

【讨论】:

  • 嗯,我最初是这样的,没有RelativeLayout 基本布局,但这是一种有趣的方式。我认为 RelativeLayout 的重点是避免做你刚刚做的事情。帮我一个忙,用更长的文字试试。这并没有真正复制我的例子。
  • 添加了长文本。如果这不是你要找的,你能在你原来的帖子中澄清一点吗?谢谢
  • 在顶部查看我的编辑。我应该清楚那不是我想要的。对不起!
  • 这很奇怪……哈哈。一秒。让我看看我能不能明白你的意思。
  • 哈哈,是的。对于那个很抱歉。希望我的解释是有道理的。如果不告诉我。
【解决方案2】:

使用 Spannables

我已经离开对话来摆弄 Spannables 并创建了这个:

public class Example extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView textView = new TextView(this);
        textView.setTextSize(18);

        String first = "A sentence that contains enough balderdash, blather and babble to wrap at least once.";
        String second = " (Normal)";

        Spannable span = new SpannableString(first + second);
        span.setSpan(new StyleSpan(Typeface.BOLD), 0, first.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new RelativeSizeSpan(0.7f), first.length(), span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setText(span);
        setContentView(textView);
    }
}

但是 prolink007 已经给了你一个通用链接......哦,好吧。反正我要发了。

【讨论】:

  • 有趣的方法。让我试试看。
  • 如果您希望其中一个不会环绕,即都是一个单词文本,那么这看起来有点傻。
  • 我会发布一张我按照你说的做的图片。它看起来不像预期的那样。但是感谢您的时间:)
  • 还有很多其他方法可以做到这一点。问题是我不确定你期待什么...
  • 较小的 TextView 紧挨着 Bigger TextView,但在第二行。
【解决方案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:id="@+id/news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:singleLine="true"
        android:ellipsize="end"
        android:textSize="30sp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        android:textStyle="bold"
        android:text="hiangdnfaljiadnk" />

    <TextView
        android:id="@+id/news_summary"
        android:paddingTop="5dp"
        android:layout_below="@id/news_title"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"             android:text="hiangdnfaljiadnkfjfaingaldnfasidognkahiangdnfaljiadnkfjfaingaldnfasidognkahiangdnfaljiadnkfjfaingaldnfasidognka"
    />

    <TextView
        android:id="@+id/news_from"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:layout_below="@id/news_summary"
        android:layout_alignParentLeft="true"
        android:textSize="20sp"
        android:text="1111111" />

    <TextView
        android:id="@+id/news_comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="10dp"
        android:layout_below="@id/news_summary"
        android:layout_alignParentRight="true"
        android:textSize="20sp"
        android:text="3333333" />

</RelativeLayout>

【讨论】:

  • 您应该在答案中添加一些描述以便更好地理解
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-11-11
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多