ボタンのテキストサイズをボタンの高さに合わせる方法を知りたいです。sp または dp サイズのテキストを使用する場合、小さな画面解像度 (たとえば 4 インチの画面) には適していますが、タブレット画面 (10 インチ) ではボタンのサイズが大きくなり、ボタンのテキストが非常に小さく見えます) どうすればこれを解決できますか?
4 に答える
onDraw()
Button の子でメソッドをオーバーロードできます。この例getTextBounds()
は、Android Paint クラスから使用して、描画時のテキストのサイズを決定する方法を示しています。この情報を使用して、必要なサイズのテキストを取得するために使用する 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
2 つのメソッドを呼び出す必要があります。まず、textSize を決定するためgetTextBounds()
に、Paint オブジェクトのメソッドを使用して、既知の textSize でテキストのサイズを決定します。そこから簡単な計算を行い、必要なテキスト境界を得るために使用される 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);
}
2 番目のメソッドは、同じ方法で textScaleX 値を決定します。a が 1.0 の場合のサイズtextScaleX
を特定し、簡単な計算を行って、必要な境界の幅になるスケールを決定します。これは、textSize の設定がスケールの結果に影響する場合に備えて、テキスト サイズの計算とは別の呼び出しで行われます。変更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 によって返される境界矩形は、これに役立ちます。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);
}
ボタンの高さに応じてボタンのテキストサイズを設定してみてください。
例: XML では、ボタンの重みを使用してすべての画面を埋めます (画面サイズに関係なく):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
onwindowfocuschanged をオーバーライドしてレイアウトを描画した後、ボタン テキストの高さ (たとえば、ボタンの高さの 1/3) を設定します。
@Override
public void onWindowFocusChanged (boolean hasFocus) { // the layout is set
button1 = (Button)findViewById(R.id.button1);
int text_height = button1.getHeight()/3; // define text_height as 1/3 of button height in px
button1.setTextSize(TypedValue.COMPLEX_UNIT_PX, text_height); // set text height in px
button2 = (Button)findViewById(R.id.button2);
int text_height = button2.getHeight()/3;
button2.setTextSize(TypedValue.COMPLEX_UNIT_PX, text_height);
}
私はそれを理解します!
フォルダーを作成しました: values-ldpi
値 - 小
値-正常
値 - 大
値-特大
各フォルダーに dimens.xml ファイルを作成しました。
たとえば、values-small の dimens.xml ファイルは次のようになります。
<resources>
<dimen name="text_button">10sp</dimen>
</resources>
次のようなvalues-normalのdimens.xmlファイル:
<resources>
<dimen name="text_button">12sp</dimen>
</resources>
次に、ボタンのテキスト サイズを unit(10sp,10px,10dp) で使用する代わりに、text_button にポイントします。
sp と dp の比較で良いことのために、layout_height で wrap_content を使用します。