2016-02-26 13 views
5

私はユーザー名とパスワードのフィールドを持つログインウィンドウを作成しています。それぞれのアイコンを入れたいと思います。 EditTextフィールドにアイコンを追加すると、アイコンのサイズを変更できません。EditText内のアイコンのサイズを変更することは可能ですか?

EditTextの背景にフォーカスがあるときに背景を変更したいので、アイコンはEditTextフィールドの内側にあります。

LinearLayout内にアイコンとEditTextビューを配置しようとしましたが、EditTextがフォーカスされているときにレイアウトの背景を変更できませんでした。

私のEditTextコード:視覚的

<EditText 
    android:id="@+id/user_name_edit_text" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="35dp" 
    android:layout_marginRight="35dp" 
    android:layout_marginTop="35dp" 
    android:background="@drawable/custom_border_selector" 
    android:drawablePadding="5dp" 
    android:hint="@string/username" 
    android:singleLine="true" 
    android:textColor="#757575" 
    android:textSize="15sp" 
    android:drawableLeft="@drawable/ic_person_grey_24dp" 
    /> 

のEditText:

enter image description here

+0

このリンクをチェックします。http://stackoverflow.com/questions/20250638/how-to-create-edittext-hint-as-text-with-image-in-android – Mrunal

+0

@ Rohit5k2それは愚かな音が鳴りますしかし、私はそれを考えなかった。ありがとう。もう1つ質問がありますが、イメージの余白や余白を追加する方法が見つかりませんでしたか?私はそれをGoogleにしようと、数時間と有用な情報はありません。 – JonasSeputis

+0

@JonasSeputis:確認してください。 – Rohit5k2

答えて

5

あなたはアイコンを変更し、焦点に小さい方を使用することができます。これはあなたが使うべき方法です。

public void setCompoundDrawablesWithIntrinsicBounds (
             int left, int top, int right, int bottom) 

、及びテキストの下の右側に、上記の左側に表示される(もしあれば)描画可能を設定します。そこにDrawableを必要としない場合は0を使います。 Drawablesの境界は、その本来の境界に設定されます。

あなたは化合物のドローアブルとテキストの間のパディングのサイズを設定しますこの

public void setCompoundDrawablePadding (int pad) 

を使用することができますパディングを追加します。

関連XML属性:

android:drawablePadding 

詳しい情報herehereを。あなたが興味をそそるかもしれない他の多くの方法があります。

10

あなたはXMLで問題を解決したい場合は、ここでは、あなたの画像サイズ/パディングを操作できるサンプルコードがあります:

enter image description here

<!-- USERNAME INPUT --> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/edit_text_selector"> 

    <!-- INPUT --> 
    <EditText 
     android:id="@+id/username_input" 
     android:layout_marginLeft="-10dp" 
     android:layout_toRightOf="@+id/username_icon" 
     android:text="" 
     android:hint="Username" 
     android:padding="10dp" 
     android:background="" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <!-- ICON --> 
    <ImageView 
     android:padding="3dp" 
     android:id="@+id/username_icon" 
     android:src="@drawable/perm_group_personal_info" 
     android:layout_width="40dp" 
     android:layout_height="40dp" /> 

</RelativeLayout> 

そして、ここではそれがどのように見えるかです

1

Javaで設定するよりも良い方法は、レイヤードドローイングの中で次のように設定することです。

Layered list drawable is as under : 

    <?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 

     > 
     <shape> 
      <solid android:color="@color/bg_row_drop_completed"/> 
      <size android:width="96dp" 
       /> 
      <padding android:top="-15dp" 
       android:bottom="-15dp"/> 
      <corners android:topLeftRadius="@dimen/btn_add_bg_radius_purple" android:topRightRadius="@dimen/btn_add_bg_radius_purple"/> 
     </shape> 
    </item> 
    <item 
     > 
     <bitmap android:src="@drawable/_ic_arrow_drop_up_normal" android:gravity="center_vertical" 
      ></bitmap> 
    </item> 
</layer-list>[enter image description here][1] 
+0

API 19で動作しないようですか? – Sam

関連する問題