Android 动态按钮文本大小
我想知道如何使按钮文本大小填充按钮的高度。当使用文本的sp或dp大小时,它有利于小屏幕分辨率(例如4英寸屏幕),但在平板电脑屏幕(10英寸)上,按钮大小更大,按钮文本看起来很小)我如何解决这个问题?使用布局高度中的换行内容,与sp和dp相比,这是一个很好的选择。您可能会过载Android 动态按钮文本大小,android,Android,我想知道如何使按钮文本大小填充按钮的高度。当使用文本的sp或dp大小时,它有利于小屏幕分辨率(例如4英寸屏幕),但在平板电脑屏幕(10英寸)上,按钮大小更大,按钮文本看起来很小)我如何解决这个问题?使用布局高度中的换行内容,与sp和dp相比,这是一个很好的选择。您可能会过载onDraw()方法。这显示了如何使用android Paint类中的getTextBounds()来确定绘制时文本的大小。根据这些信息,您可以计算textSize和textScaleX,以获得所需的文本大小 目标是找到tex
onDraw()
方法。这显示了如何使用android Paint类中的getTextBounds()
来确定绘制时文本的大小。根据这些信息,您可以计算textSize和textScaleX,以获得所需的文本大小
目标是找到textSize
和textScaleX
的值,它们将调整文本大小以填充视图。我们不会每次调用onDraw时都计算这个值,而是观察大小的变化并预先计算值。为此,我们覆盖onSizeChanged以获得新的视图宽度和高度
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// save view size
mViewWidth = w;
mViewHeight = h;
// first determine font point size
adjustTextSize();
// then determine width scaling
// this is done in two steps in case the
// point size change affects the width boundary
adjustTextScale();
}
从onSizeChanged
必须调用两个方法。首先要确定文本大小,我们使用Paint对象的getTextBounds()
方法确定已知文本大小的文本大小。在此基础上,我们进行一个简单的计算,以确定将使用什么textSize来生成我们想要的文本边界
void adjustTextSize() {
mTextPaint.setTextSize(100);
mTextPaint.setTextScaleX(1.0f);
Rect bounds = new Rect();
// ask the paint for the bounding rect if it were to draw this
// text
mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);
// get the height that would have been produced
int h = bounds.bottom - bounds.top;
// make the text text up 70% of the height
float target = (float)mViewHeight*.7f;
// figure out what textSize setting would create that height
// of text
float size = ((target/h)*100f);
// and set it into the paint
mTextPaint.setTextSize(size);
}
第二种方法以相同的方式确定textScaleX值。我们找到textScaleX
为1.0时的大小,然后做一些简单的数学计算,确定什么比例会产生我们想要的边界宽度。这在文本大小计算的单独调用中完成,以防设置文本大小影响缩放结果。更改textScaleX
绝对不会更改所绘制文本的高度
void adjustTextScale() {
// do calculation with scale of 1.0 (no scale)
mTextPaint.setTextScaleX(1.0f);
Rect bounds = new Rect();
// ask the paint for the bounding rect if it were to draw this
// text.
mTextPaint.getTextBounds(mText, 0, mText.length(), bounds);
// determine the width
int w = bounds.right - bounds.left;
// calculate the baseline to use so that the
// entire text is visible including the descenders
int text_h = bounds.bottom-bounds.top;
mTextBaseline=bounds.bottom+((mViewHeight-text_h)/2);
// determine how much to scale the width to fit the view
float xscale = ((float) (mViewWidth-getPaddingLeft()-getPaddingRight())) / w;
// set the scale for the text paint
mTextPaint.setTextScaleX(xscale);
}
最后一个细节是确定要在视图中垂直绘制文本的位置。getTextBounds返回的boundary rect可以帮助实现这一点。bounds.top值实际上是负值,它表示文本的大小,不包括任何下一级的大小(延伸到基线以下的字母部分)。bounds.bottom值是下行程序的大小。使用这些信息,我们可以决定如何在视图中定位文本。调用drawText中的y值表示基线。下降线将绘制在此线下方。在本例中,我们调整y值,以便显示完整的下降部分。此值保存为mTextBaseline
,并在onDraw中使用
@Override
protected void onDraw(Canvas canvas) {
// let the ImageButton paint background as normal
super.onDraw(canvas);
// draw the text
// position is centered on width
// and the baseline is calculated to be positioned from the
// view bottom
canvas.drawText(mText, mViewWidth/2, mViewHeight-mTextBaseline, mTextPaint);
}
我明白了
我创建了以下文件夹:
价值观
价值小
正常值
价值大
值xlarge
在每个文件夹中,我创建了dimens.xml文件
例如,值较小的dimens.xml文件如下所示:
<resources>
<dimen name="text_button">10sp</dimen>
</resources>
<resources>
<dimen name="text_button">12sp</dimen>
</resources>
10便士
dimens.xml文件的正常值如下:
<resources>
<dimen name="text_button">10sp</dimen>
</resources>
<resources>
<dimen name="text_button">12sp</dimen>
</resources>
12便士
然后,您只需将其指向text_按钮,而不是将按钮文本大小与单元(10sp、10px、10dp)一起使用 您可以尝试设置相对于按钮高度的按钮文本大小 例如:在XML中,使用按钮权重填充所有屏幕(无论屏幕大小): 可能重复的