Android textview 的view bounds 比content 大很多,怎么去掉?
Android textview's view bounds are much larger than content, how to remove it?
我的问题是,当我打开开发人员选项中的设置以查看视图边界时,我可以看到橙色的“20”文本换行 space 比需要的大得多。 (顶部和底部)
我尝试设置:android:includeFontPadding="false"
但它只是简单地向上推内容,在文本容器底部留下一个很大的空白 space。
如何删除多余的 space?
XML:
<LinearLayout
android:id="@+id/topDash"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/circle_gray"
android:orientation="vertical" >
<TextView
android:id="@+id/RobotoTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/sog_C"
android:textColor="@color/blue_medium"
android:textSize="20sp" />
<TextView
android:id="@+id/RobotoTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="20"
android:textColor="@color/orange"
android:textSize="100sp" />
<TextView
android:id="@+id/robotoTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="KM/H"
android:textColor="@color/gray_darker"
android:textSize="24sp" />
</LinearLayout>
非常好的问题。我不确定是否可以只在 XML 中并且不为 scrollY 属性 设置任何硬编码值,但我实现了一个没有硬编码值的小自定义 class,这您可以在其他地方不做任何修改地使用。在这种情况下,onMeasure
方法总能派上用场 :) 我在这里使用默认字体。如果您使用不同的字体,请调用 textPaint.setTypeface()
并将其设置在 textPaint.getTextBounds
语句之前。
public class CustomTextView extends TextView {
private int newWidth;
private int newHeight;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(newHeight == 0) {
Rect textBounds = new Rect();
Paint textPaint = new Paint();
textPaint.setTextSize(getTextSize());
textPaint.getTextBounds(getText().toString(), 0, getText().length(), textBounds);
int descent = (int) textPaint.descent();
newWidth = textBounds.width();
newHeight = textBounds.height() - 2 * descent;
setScrollY(descent);
}
setMeasuredDimension(newWidth, newHeight);
}
}
但是这个解决方案只适用于数字,为什么只适用于数字,因为下降,这就是为什么我们不能删除 XML 里面的额外 space,它是一种字体 属性
我的问题是,当我打开开发人员选项中的设置以查看视图边界时,我可以看到橙色的“20”文本换行 space 比需要的大得多。 (顶部和底部)
我尝试设置:android:includeFontPadding="false"
但它只是简单地向上推内容,在文本容器底部留下一个很大的空白 space。
如何删除多余的 space?
XML:
<LinearLayout
android:id="@+id/topDash"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/circle_gray"
android:orientation="vertical" >
<TextView
android:id="@+id/RobotoTextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/sog_C"
android:textColor="@color/blue_medium"
android:textSize="20sp" />
<TextView
android:id="@+id/RobotoTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="20"
android:textColor="@color/orange"
android:textSize="100sp" />
<TextView
android:id="@+id/robotoTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="KM/H"
android:textColor="@color/gray_darker"
android:textSize="24sp" />
</LinearLayout>
非常好的问题。我不确定是否可以只在 XML 中并且不为 scrollY 属性 设置任何硬编码值,但我实现了一个没有硬编码值的小自定义 class,这您可以在其他地方不做任何修改地使用。在这种情况下,onMeasure
方法总能派上用场 :) 我在这里使用默认字体。如果您使用不同的字体,请调用 textPaint.setTypeface()
并将其设置在 textPaint.getTextBounds
语句之前。
public class CustomTextView extends TextView {
private int newWidth;
private int newHeight;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if(newHeight == 0) {
Rect textBounds = new Rect();
Paint textPaint = new Paint();
textPaint.setTextSize(getTextSize());
textPaint.getTextBounds(getText().toString(), 0, getText().length(), textBounds);
int descent = (int) textPaint.descent();
newWidth = textBounds.width();
newHeight = textBounds.height() - 2 * descent;
setScrollY(descent);
}
setMeasuredDimension(newWidth, newHeight);
}
}
但是这个解决方案只适用于数字,为什么只适用于数字,因为下降,这就是为什么我们不能删除 XML 里面的额外 space,它是一种字体 属性