ご存じのように、TextInputLayout
は、プライベートヘルパークラスを使用して、ヒントテキストのスタイルやアニメーションを処理します。このクラス - CollapsingTextHelper
- は、のために別の書体を維持して、とを展開しました。州です。私たちは正しいものを設定する必要があります。これは、リフレクションを使用して行います。
私は通常、この種の機能をカスタムサブクラスにパッケージ化していますので、ここでも同じことをします。サブクラスを使用したくない場合、反射物は簡単にあなたのActivity
またはユーティリティクラスに入れることができるいくつかの簡単な方法に引き込まれる可能性があります。やや反し
public class CustomTextInputLayout extends TextInputLayout {
private Object collapsingTextHelper;
private Method setCollapsedTypefaceMethod;
private Method setExpandedTypefaceMethod;
public CustomTextInputLayout(Context context) {
this(context, null);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
try {
Field cthField = TextInputLayout.class
.getDeclaredField("mCollapsingTextHelper");
cthField.setAccessible(true);
collapsingTextHelper = cthField.get(this);
setCollapsedTypefaceMethod = collapsingTextHelper
.getClass().getDeclaredMethod("setCollapsedTypeface", Typeface.class);
setCollapsedTypefaceMethod.setAccessible(true);
setExpandedTypefaceMethod = collapsingTextHelper
.getClass().getDeclaredMethod("setExpandedTypeface", Typeface.class);
setExpandedTypefaceMethod.setAccessible(true);
}
catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
collapsingTextHelper = null;
setCollapsedTypefaceMethod = null;
setExpandedTypefaceMethod = null;
e.printStackTrace();
}
}
public void setCollapsedTypeface(Typeface typeface) {
if (collapsingTextHelper == null) {
return;
}
try {
setCollapsedTypefaceMethod.invoke(collapsingTextHelper, typeface);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
public void setExpandedTypeface(Typeface typeface) {
if (collapsingTextHelper == null) {
return;
}
try {
setExpandedTypefaceMethod.invoke(collapsingTextHelper, typeface);
}
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
、TextInputLayout
のヒントがEditText
上記フローティングラベルである場合状態が崩壊しました。そのの状態がヒントがEditText
の中の「通常の」位置にあるときです。両方の状態の書体を設定するメソッドは、上で与えられています。
これはTextInputLayout
のドロップイン代替品で、レイアウトで使用することができます。コードで
<com.mycompany.myapp.CustomTextInputLayout
android:id="@+id/username_til"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/TextLabel">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:hint="Username" />
</com.mycompany.myapp.CustomTextInputLayout>
、フローティングテキストヒントの書体を設定する:例えば
CustomTextInputLayout usernameTextInputLayout =
(CustomTextInputLayout) findViewById(R.id.username_til);
usernameTextInputLayout.setCollapsedTypeface(Utils.ROBOTO_BOLD_TYPE_FACE);
上記使用CollapsingTextHelper
方法は、支持体のバージョン23.1.0で追加されましたとしょうかん。以前のバージョンを使用している場合や、何らかの理由でNoSuchMethodException
が表示されている場合は、バージョンに関係なく、書体フィールドを直接設定するthe original version of my answerを直接設定してください。
はい、可能ですが、私はそれが反映されると確信しています。私は最近、カスタムの 'TextInputLayout'サブクラスを作成しました。私の頭の上から、私はそれを反映させる方法を考えることができません。それが受け入れられるなら、私はおそらく何かを一緒に置くことができます。 –
私は反射テクニックを試しました。http://stackoverflow.com/a/30767869/72437ヒントテキストにも影響します。 –
ええと、私はそれができると確信しています。少し試してみてください。私はああ知らせます。 –