2016-10-23 10 views
1

現在、私は浮動テキストであるInputTextLayoutに大胆な効果を与えたいと思います。ここには私がやっていることがありますInputTextLayoutの浮動テキストとヒントテキストに異なるフォントを使用することは可能ですか

this.usernameTextInputLayout.setTypeface(Utils.ROBOTO_BOLD_TYPE_FACE); 

期待どおりに動作します。フローティングテキスト(ユーザー名)は太字になっています。

enter image description here

しかし、これは私のために、他の望ましくない効果を作成します。ヒントのテキストも太字になります。

enter image description here

あなたは上記の2つの画像を比較することができます。比較のために、私はちょうどpasswordTextInputLayoutのままにしておきます。

浮動テキストとヒントテキストが異なるフォントを使用することはできますか?InputTextLayout

+0

はい、可能ですが、私はそれが反映されると確信しています。私は最近、カスタムの 'TextInputLayout'サブクラスを作成しました。私の頭の上から、私はそれを反映させる方法を考えることができません。それが受け入れられるなら、私はおそらく何かを一緒に置くことができます。 –

+0

私は反射テクニックを試しました。http://stackoverflow.com/a/30767869/72437ヒントテキストにも影響します。 –

+0

ええと、私はそれができると確信しています。少し試してみてください。私はああ知らせます。 –

答えて

3

ご存じのように、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を直接設定してください。

+0

コードをありがとう。私は、ヒントテキストが 'EditText'のフォントに従わないことを認識しています。しかし、 'mExpandedTypeface'でリフレクションを使うことで、ヒントテキストのフォントも変更できます。 –

+1

ああ、そうですね?私は再び源を見なければならないでしょう。私はおそらく私のテーマで同じものに設定していただろうし、私はちょうど他のものから設定されていたと思っていた。情報をありがとう!コードをもう一度見直して、回答が修正されたかどうかを確認します。あなたがうまく働いてうれしい。乾杯! –

+0

私はちょうど最新のソースを掘り下げましたが、ヒント書体は 'EditText'のものから設定されているようですが、スタイルは' NORMAL'に限定されています。私は、インフレーション後に実行時に 'EditText'の書体を設定している可能性があることに気付きました。その場合、あなたは正しいです。 'TextInputLayout'はその書体を変更しません。 'EditText'が最初に追加されたときにのみそれ自身を初期化します。その後、 'EditText'で行った変更には反応しません。ヒント文字列や色などは同じです。 –

関連する問題