简体   繁体   中英

How to set indentation for first line of TextView according to another's width

I am trying to create following list view item from Google's Material Design Guildlines for list components.

So, it should consist of different views as described:

My try:

I don't think it is possible to set margin for only first line of TextView.

With manual indentation:

android:text="                                         - I'll be in your neighborhood doing errands this weekend. Do you want to join us?"

How can I set indentation for TextView Content considering it displays HTML formatted code.

list_item.xml (if you wonder)

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

Take out the user_fullName view. You will be putting everything in the content view.

You can have multiple font styles in the same TextView . You will use that to get this appearance.

Unfortunately, you can't specify markup in the layout XML so you can't see how the text will look in preview mode. You have to do it in code:

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

or another approach:

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM