3.0でNumberPickerウィジェットを取得しましたが、このウィジェットのtextSizeは変更できないようです。私は何かが足りないのですか、それともそうですか?フォントサイズを大きくしたいのですが、デフォルト値ではかなり小さくなっています。しかし、そのためのtextSizeプロパティが表示されません。
同じ問題に遭遇し、 NumberPicker をローカルで再作成しました。より詳細なチュートリアル here があります。
デフォルトのNumberPickerを拡張することでこれを実現できました。理想的ではありませんが、動作します。
public class NumberPicker extends Android.widget.NumberPicker {
public NumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void addView(View child) {
super.addView(child);
updateView(child);
}
@Override
public void addView(View child, int index, Android.view.ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
@Override
public void addView(View child, Android.view.ViewGroup.LayoutParams params) {
super.addView(child, params);
updateView(child);
}
private void updateView(View view) {
if(view instanceof EditText){
((EditText) view).setTextSize(25);
((EditText) view).setTextColor(Color.parseColor("#333333"));
}
}
}
次に、このクラスをレイアウトxmlで参照します。
<com.yourpackage.NumberPicker
Android:id="@+id/number_picker"
Android:layout_width="43dp"
Android:layout_height="wrap_content" />
同じ問題に直面していました。NumberPickerのテキストサイズを大きくしたかったのですが、ウィジェットの一部としてそれを行う方法が見つかりませんでした。上記のすべての答えは素晴らしいですが、代わりに私は次の解決策を採用しました。
<NumberPicker
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:scaleX="1.5"
Android:scaleY="1.5"/>
これは本質的にウィジェット全体を拡大します。
numberPickerにこのスタイルを設定するだけです。
<style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
<item name="Android:textColorPrimary">@Android:color/white</item>
<item name="Android:textSize">20dp</item>
</style>
<NumberPicker
Android:theme="@style/AppTheme.Picker"
Android:id="@+id/yearNumberPicker"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content" />
および<item name="Android:textColorPrimary">@Android:color/white</item>
change textcolor:
以下は私のために働きました(API 15/4.0.3で)
注: aheuermannのソリューションは、さまざまなAndroidバージョン間でのNumberPicker
のレイアウトの実装の潜在的な違いに対して安全です(つまり、TextView
が常に子の位置1にあるとは限りません)。
TextView tv1 = (TextView)_numberPicker.getChildAt(1);
tv1.TextSize = 10;
API 23にジャンプすると、aheuermannによって提示されたソリューションには問題があるようです。 NumberPickerのソースでは、テキストサイズは初期化時にEditText子に基づいて設定されます。後で変更しようとすると、EditTextフィールドは大きくなりますが、ウィジェットによって直接描画される他のすべての数値は依然として小さくなります。
私が行っているソリューションには、レイアウトではなくプログラムでインスタンス化する必要があるという欠点がありますが、それ以外の場合は、意図したとおりに機能するようです。まず、styles.xmlファイルに以下を追加します。
<style name="NumberPickerText">
<item name="Android:textSize">40dp</item>
</style>
次に、NumberPickerをインスタンス化して
ContextThemeWrapper cw = new ContextThemeWrapper(this, R.style.NumberPickerText);
NumberPicker np = new NumberPicker(cw);
アクティビティ内で行われていると仮定します。そうでない場合は、「this」を使用可能な任意のコンテキストに置き換えます。このコードは、アクティビティ内のすべてのTextViewサイズをオーバーライドするテーマを設定することと同等ですが、NumberPickerとその子にのみ適用されます。
NumberPicker内のビューをカスタマイズできます。
3つのビューがあります-
2つのImageButttonsと1つのEditText。
if(numberPicker.getChildAt(1) instanceOf EditText)
{
EditText edt=(EditText)numberPicker.getChildAt(1);
//do customizations here
}
AndroidソースコードからのNumberPickerの完全なレイアウト
<?xml version="1.0" encoding="utf-8"?>
<!--
**
** Copyright 2008, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.Apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<merge xmlns:Android="http://schemas.Android.com/apk/res/Android">
<ImageButton Android:id="@+id/increment"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:background="@Android:drawable/numberpicker_up_btn"
Android:paddingTop="22dip"
Android:paddingBottom="22dip"
Android:contentDescription="@string/number_picker_increment_button" />
<EditText
Android:id="@+id/numberpicker_input"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:textAppearance="@style/TextAppearance.Large.Inverse.NumberPickerInputText"
Android:gravity="center"
Android:singleLine="true"
Android:background="@drawable/numberpicker_input" />
<ImageButton Android:id="@+id/decrement"
Android:layout_width="fill_parent"
Android:layout_height="wrap_content"
Android:background="@Android:drawable/numberpicker_down_btn"
Android:paddingTop="22dip"
Android:paddingBottom="22dip"
Android:contentDescription="@string/number_picker_decrement_button" />
</merge>