无法按宽度包装任意行数的blockquote-like TextView

Act*_*ine 4 android textview android-layout

我需要渲染任意长度的引用块.文本必须与左边对齐,而块本身与右边对齐,类似于这个: 例

对于我正在尝试一种TextViewandroid:width="wrap_content",android:gravity="start"android:layout_gravity="end".但是,只有当文本适合单行时,这才能正常工作 - 如果文本长TextView于此行,则行为如下:

  • 第一个引用块只是一个带空格的句子 - 吞噬所有父级的宽度;
  • 第二个街区 - 一些空间没有破坏:Raw persistence may be the only option other than giving up entirely.- 仍然是块的行为match_parent.
  • 第3个块使用显式换行符 - 看起来最接近需要的换行符,但这是最不灵活的选项,并且右侧还有一些额外的填充.

截图

这是布局(paddingRight替换layout_marginRight为突出目的 - 行为是相同的两种方式):

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

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:lineSpacingExtra="3.5dp"
            android:paddingLeft="24dp"
            android:layout_marginRight="24dp"
            android:text="Raw persistence may be the only option other than giving up entirely."
            />

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:lineSpacingExtra="3.5dp"
            android:paddingLeft="24dp"
            android:layout_marginRight="24dp"
            android:text="Raw&#160;persistence may&#160;be the only&#160;option other&#160;than giving&#160;up&#160;entirely."
            />

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:lineSpacingExtra="3.5dp"
            android:paddingLeft="24dp"
            android:layout_marginRight="24dp"
            android:text="@string/Raw persistence may be the only option\nother than giving up entirely."
            />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有没有办法充分地解决这个问题,而不必为不同的设备宽度等采用多个字符串?= \

Act*_*ine 6

好的,经过对TextView代码的一些检查,我把解决方案整合到了我需要的东西:

package com.actinarium.persistence.common;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Layout;
import android.util.AttributeSet;
import android.widget.TextView;

public class BlockquoteTextView extends TextView {
    public BlockquoteTextView(Context context) {
        super(context);
    }

    public BlockquoteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BlockquoteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BlockquoteTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Now fix width
        float max = 0;
        Layout layout = getLayout();
        for (int i = 0, size = layout.getLineCount(); i < size; i++) {
            final float lineWidth = layout.getLineMax(i);
            if (lineWidth > max) {
                max = lineWidth;
            }
        }

        final int height = getMeasuredHeight();
        final int width = (int) Math.ceil(max) + getCompoundPaddingLeft() + getCompoundPaddingRight();

        setMeasuredDimension(width, height);
    }
}
Run Code Online (Sandbox Code Playgroud)

默认实现onMeasure不考虑行宽,除非它们按\n's(代码)细分.我用getLineMax而不是getLineWidth因为后者测量尾随空格 - 原始帖子中块#3中未对齐的罪魁祸首.