【问题标题】:Android textview's view bounds are much larger than content, how to remove it?Android textview的view bounds比content大很多,怎么去掉?
【发布时间】:2015-02-26 14:40:10
【问题描述】:

我的问题是,当我打开开发人员选项中的设置以查看视图边界时,我可以看到橙色的“20”文本包含的空间比要求的要大得多。 (上下)

我尝试设置:android:includeFontPadding="false",但这只是简单地将内容向上推,在文本容器底部留下一个很大的空白空间。

如何删除多余的空间?

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>

【问题讨论】:

  • 据我所知,除了用尺寸改变 textview 高度之外,没有其他解决方案。

标签: android textview


【解决方案1】:

非常好的问题。我不确定是否可以仅在 XML 中执行此操作,并且不为 scrollY 属性设置任何硬编码值,但我实现了一个没有硬编码值的小型自定义类,您可以在其他地方使用它而无需任何修改。在这种情况下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 中的多余空格,它是一个字体属性

【讨论】:

  • 下降...我现在明白了。谢谢你的好回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2014-08-05
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 2018-09-13
相关资源
最近更新 更多