Act*_*ine 4 android textview android-layout
我需要渲染任意长度的引用块.文本必须与左边对齐,而块本身与右边对齐,类似于这个:

对于我正在尝试一种TextView用android: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.这是布局(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 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="@string/Raw persistence may be the only option\nother than giving up entirely."
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
有没有办法充分地解决这个问题,而不必为不同的设备宽度等采用多个字符串?= \
好的,经过对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中未对齐的罪魁祸首.
| 归档时间: |
|
| 查看次数: |
771 次 |
| 最近记录: |