2016-01-08 6 views
6

EditTextの左Drawableの上にTextInputLayoutラベルが垂直に表示されるようにすることはできますか?ここでTextInputLayout EditTextの左Drawableの上にあるラベル

はのEditTextのxmlです:ここで

<android.support.design.widget.TextInputLayout 
       android:id="@+id/completion_date_layout2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_weight="1" 
       android:orientation="horizontal"> 

       <EditText 
        android:id="@+id/etTaskDate" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:hint="@string/title_completion_date" 
        android:inputType="text" 
        android:drawableLeft="@drawable/ic_date" 
        android:paddingBottom="15dp" 
        android:drawablePadding="5dp" 
        android:textSize="@dimen/fields_text_size"/> 
      </android.support.design.widget.TextInputLayout> 

は、所望の出力である:ここでは

enter image description here

は、私が取得しています出力されます:

enter image description here

+2

あなたは解決策を見つけましたか?私は同じ問題を抱えている。 –

+0

@RaymonddelaCroix残念ながら先月から私の友人はいません。私はその回避策の下に投稿しました。 :) – icaneatclouds

答えて

3

ありがとう:

package android.support.design.widget; 

import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 

public final class TextInputLayoutEx extends TextInputLayout { 

    private final int mDefaultPadding = __4DP__; 
    private final Rect mTmpRect = new Rect(); 

    public TextInputLayoutEx(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     if (isHintEnabled() && mEditText != null) { 
      final Rect rect = mTmpRect; 
      ViewGroupUtils.getDescendantRect(this, mEditText, rect); 
      mCollapsingTextHelper.setCollapsedBounds(
       rect.left + mDefaultPadding, getPaddingTop(), 
       rect.right - mDefaultPadding, bottom - top - getPaddingBottom()); 
      mCollapsingTextHelper.recalculate(); 
     } 
    } 

} 

ここでは、我々は、新しいクラスを置きますパッケージレベルの可視性を持つmCollapsingTextHelperへのアクセスを開き、フィールド名の配置を管理する元のonLayoutメソッドのコードの一部を繰り返します。 __4DP__の値は4dpの値でピクセルに変換されています。私は誰もがこのためのユーティリティメソッドを持っていると確信しています。あなたのxmlレイアウトで

がちょうどandroid.support.design.widget.TextInputLayoutからandroid.support.design.widget.TextInputLayoutExに切り替えるので、あなたのレイアウトは次のようになります。

<android.support.design.widget.TextInputLayoutEx 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Mobile"> 
    <android.support.design.widget.TextInputEditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/ic_phone_black_24dp" 
     android:drawablePadding="4dp"/> 
</android.support.design.widget.TextInputLayoutEx> 

そしてその結果は、それがcom.android.support:design:25.3.1

のために働く瞬間

Collapsed and expanded state

です

+0

別のプロジェクトでこれを試してみましょう。ありがとう – icaneatclouds

+0

EPICは、魅力的に働いた。 @icaneatcloudsは、このコードを「答え」としてマークすることを検討します。 – MiguelCatalan

-1

私は、この問題に関してd。それは少し信頼できないかもしれませんが、それは私の最後に動作します。ここ はコードです:

String spacer="  "; 
EditText myEditText= (EditText)findViewById(R.id.myEditText); 
myEditText.setText(spacer + "Today"); 
myEditText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_calendar, 0, 0, 0); 

私はここでやったことは、プログラムで左描画可能を追加し、EDITTEXT内のテキストの前にスペーサーを置きました。

しかしEDITTEXTの内容をフェッチする前に、これらのスペースを削除してください:

String title = myEditText.getText().toString().substring(8); 

これは私が単語「今日」の前に8つのスペースをトリミング意味。

+0

あなたはdownvoteを説明できますか?私の質問には有効な答えがありませんので、すばやく回避策を講じました。 – icaneatclouds

2

アニメーションとframe_layoutを使用して、左のアイコンをアニメーション化することができます。thisリンクを試してみると便利です。それは元のコードの最小値を繰り返し、簡単なサブクラス化を達成することができ、小さな抜け穴を残したJavaのメンバーアクセスモデルとGoogleの開発者に

関連する問題