我正在尝试从 Google's Material Design Guildlines 创建以下 ListView 项用于列表组件。
因此,它应该由描述的不同 View 组成:
我的尝试:
我认为不可能只为 TextView 的第一行设置边距。
手动缩进:
android:text=" - I'll be in your neighborhood doing errands this weekend. Do you want to join us?"
考虑到 TextView Content
显示 HTML 格式的代码,我如何设置缩进。
list_item.xml(如果你想知道的话)
<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>
最佳答案
取出user_fullName
View 。您将把所有内容都放在 content
View 中。
您可以在同一个 TextView
中使用多种字体样式。您将使用它来获得这种外观。
遗憾的是,您无法在布局 XML 中指定标记,因此无法查看文本在预览模式下的外观。您必须在代码中执行此操作:
contentView.setText(Html.fromHtml("<font color=\'black\'>" + username + "</font> — " + 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);
关于android - 如何根据另一个人的宽度为TextView的第一行设置缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765462/