Android 如何根据另一个';s宽度

Android 如何根据另一个';s宽度,android,android-layout,android-studio,textview,Android,Android Layout,Android Studio,Textview,我正在尝试为列表组件创建以下列表视图项 因此,它应包括如下所述的不同视图: 我的尝试: 我认为不可能只为TextView的第一行设置边距 使用手动缩进: 如何设置TextView内容的缩进,因为它显示HTML格式的代码 list_item.xml(如果您想知道的话) 取出用户全名视图。您将把所有内容都放在内容视图中 您可以在同一TextView中使用多种字体样式。您将使用它来获得此外观 不幸的是,您无法在布局XML中指定标记,因此无法查看文本在预览模式下的外观。您必须在代码中执行此操作

我正在尝试为列表组件创建以下列表视图项

因此,它应包括如下所述的不同视图:

我的尝试:

我认为不可能只为TextView的第一行设置边距

使用手动缩进:

如何设置
TextView内容的缩进,因为它显示HTML格式的代码

list_item.xml(如果您想知道的话)
取出
用户全名
视图。您将把所有内容都放在
内容
视图中

您可以在同一
TextView
中使用多种字体样式。您将使用它来获得此外观

不幸的是,您无法在布局XML中指定标记,因此无法查看文本在预览模式下的外观。您必须在代码中执行此操作:

    contentView.setText(Html.fromHtml("<font color=\'black\'>" + username + "</font> &mdash; " + TextUtils.htmlEncode(content));

如果用户名太长怎么办?这就是它的美妙之处。不管用户名有多长,整个过程都会得到不同的颜色或样式,未设置样式的文本会紧跟其后,就像浏览器中的HTML一样。当然,在构建整个字符串之前,您可以对用户名长度设置一个限制并自己截断它。实际上,我确实认为只创建一个
TextView
,并使用html标记以不同的颜色显示用户名,但长度对我来说是个问题。如何知道何时在不同的屏幕大小上截断用户名?从右边设置边距,使用android:ellipsize=“end”你可能会发疯,从
TextView
甚至从
Layout
获得
TextPaint
来帮助你测量文本长度,但我的建议是:不要过度思考。为不同的屏幕宽度提供一些合理的长度,创建一个整数资源,即
30
,并使用不同的资源目录,即
值-w600dp
值-w820dp
,等等,为不同的屏幕宽度设置不同的值。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="84dp"
    android:paddingTop="16dp">

    <ImageView
        android:id="@+id/user_avatar"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/time_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        android:text="Saturday"
        android:textAppearance="@style/TextAppearance.AppCompat.Small" />

    <TextView
        android:id="@+id/subject_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="100dp"
        android:layout_marginLeft="72dp"
        android:layout_marginRight="100dp"
        android:layout_marginStart="72dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Brunch this weekend? Just making line long"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/subject_text"
        android:layout_alignStart="@+id/subject_text"
        android:layout_below="@+id/subject_text"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="                                         - I'll be in your neighborhood doing errands this weekend. Do you want to join us?"
        android:layout_marginRight="16dp"
        android:lineSpacingExtra="4dp"
        android:layout_marginEnd="16dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Menu" />

    <TextView
        android:id="@+id/user_fullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/subject_text"
        android:layout_alignStart="@id/subject_text"
        android:layout_below="@+id/subject_text"
        android:layout_marginEnd="150dp"
        android:layout_marginRight="150dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Ali Connors, has long name"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/content"
        android:layout_alignLeft="@+id/subject_text"
        android:layout_alignStart="@+id/subject_text"
        android:layout_marginTop="16dp"
        android:background="#9e9e9e" />

</RelativeLayout>
    contentView.setText(Html.fromHtml("<font color=\'black\'>" + username + "</font> &mdash; " + TextUtils.htmlEncode(content));
    SpannableStringBuilder ssb = new SpannableStringBuilder();
    ssb.append(username);
    ssb.setSpan(new TextAppearanceSpan(context, android.R.style.TextAppearance_Material_Menu), 0, ssb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.append(" \u2014 ");
    ssb.append(content);
    contentView.setText(ssb);