扩展为多行的TextView似乎会自动扩展以适应它的最大可能宽度。我该如何防止这种情况发生?
这是一个示例布局(test.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:background="@color/green"
android:layout_alignParentRight="true"
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="This is the message a much shorter message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
</RelativeLayout>
如果我在左边添加一个边距,它会正确缩小TextView的宽度(不重新格式化内容),但是必须根据TextView的内容计算边距的大小。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<TextView
android:layout_marginLeft="32dp"
android:background="@color/green"
android:layout_alignParentRight="true"
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="This is the message a much shorter message"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/black" />
</RelativeLayout>
答案 0 :(得分:8)
您可以使用自定义TextView和重写方法onMeasure()来计算宽度:
public class WrapWidthTextView extends TextView {
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Layout layout = getLayout();
if (layout != null) {
int width = (int)FloatMath.ceil(getMaxLineWidth(layout))
+ getCompoundPaddingLeft() + getCompoundPaddingRight();
int height = getMeasuredHeight();
setMeasuredDimension(width, height);
}
}
private float getMaxLineWidth(Layout layout) {
float max_width = 0.0f;
int lines = layout.getLineCount();
for (int i = 0; i < lines; i++) {
if (layout.getLineWidth(i) > max_width) {
max_width = layout.getLineWidth(i);
}
}
return max_width;
}
}
答案 1 :(得分:0)
使用android:padding="32dp"
如果你想从左边使用填充android:paddingLeft="32dp"
答案 2 :(得分:0)
我已经尝试了 Vitaliy Polchuk 的解决方案,它运行正常。而且我还想添加小修正:
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Layout layout = getLayout();
if (layout != null) {
int width = (int) FloatMath.ceil(getMaxLineWidth(layout))
+ getCompoundPaddingLeft() + getCompoundPaddingRight();
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// get already measured dimensions
int width = getMeasuredWidth();
int height = getMeasuredHeight();
setMeasuredDimension(width, height);
}
...
答案 3 :(得分:0)
如果您想使用@Vitaliy Polchuk的建议(最佳答案),请重写或在Why is wrap content in multiple line TextView filling parent?查看答案:
public class WrapWidthTextView extends AppCompatTextView {
public WrapWidthTextView(Context context) {
super(context);
}
public WrapWidthTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public WrapWidthTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Layout layout = getLayout();
if (layout != null) {
int width = (int) Math.ceil(getMaxLineWidth(layout))
+ getCompoundPaddingLeft() + getCompoundPaddingRight();
int height = getMeasuredHeight();
setMeasuredDimension(width, height);
}
}
private float getMaxLineWidth(Layout layout) {
float max_width = 0.0f;
int lines = layout.getLineCount();
for (int i = 0; i < lines; i++) {
if (layout.getLineWidth(i) > max_width) {
max_width = layout.getLineWidth(i);
}
}
return max_width;
}
}
将此类插入XML时,重建项目。否则它不会找到这个课程。