2013-07-24 11 views
6

私は、AndroidでsetCompoundDrawables(..)関数を使用してDrawableを追加できることを知っています。EditTextの左上隅にdrawableを追加します。

私の問題は、私のEditTextはマルチラインで、私はEditTextの左上隅にdrawableを入れたいと思っています。しかし、setCompoundDrawables()関数では、drawableを特定の側に置くことだけを選択できます。

Android APIで可能ですか?

+0

チェックこの1つ - http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds%28int,%20int,%20int,%20int%29 – g00dy

+2

so-> 'EditTextId.setCompoundDrawablesWithIntrinsicBounds(0、myIcon、0、0);のように' setCompoundDrawables(..) 'を' setCompoundDrawablesWithIntrinsicBounds() 'に置き換えることができます。 – g00dy

+0

myIconに入れることができる値は?私はそれがintであることを知っています..どちらの値を選ぶことができますか? – blay

答えて

5

EditText/TextViewとしか実行できないようです(最後に方法を提供しましたが、画像でより多くの作業が必要で、すべての電話機に適用できない可能性があります)。異なる編集テキストのバックゴンドは、モデルによって異なります)。同じ質問が既に多くの時間、例えば、herehereと尋ねられています。私の理解パー

最も簡単かつfastests道(RelativeLayoutの使用によって)上記の質問に示唆されていない可能性がありますように、ちょうどFrameLayoutを使用すると、以下の方法(あなたが+ ImageViewRelativeLayoutを使用して3ビュー対2つのビューを持っています): アイコンを9patch(9patch toolを使用)にします。例えば。あなたは最初に次のアイコンがあります。

original icon

その後、あなたは、次のいずれかに変換することができます:

enter image description here(あなたがコンテンツの表示や地域で延伸されるべき領域を示している黒画素を見ることができます)私がしました

<!-- Main activity layout --> 
    <RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     <!-- Edit text with drawable at top left --> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:background="@drawable/icon_9patch"> 
      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/edit" 
       android:hint="Enter text" 
       android:maxLines="10" 
       android:inputType="textMultiLine" /> 
     </FrameLayout> 
    </RelativeLayout> 

は、次のような結果になります(次のXMLを使用して

)複数行のテキストを追加しました:

result image

また、それだけでEditTextにタスクを遵守することも可能です(ただし、あなたは良いではないかもしれませんデフォルトのEditTextの背景を失うかもしれない、しかし、あなたはあなたの寧にそれを含めることができます - パッチ画像。 ネイティブ 1の代わりにすべての電話で同じままです)。

次のXMLも正常に動作します:

<!-- Main activity layout --> 
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <!-- Edit text with drawable at top left --> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/edit" 
      android:hint="Enter text" 
      android:maxLines="10" 
      android:inputType="textMultiLine" 
      android:layout_alignParentBottom="true" 
      android:background="@drawable/icon_9patch" /> 
</RelativeLayout> 

は(編集用のデフォルトの枠が消えていることに注意してください)以下の結果を与える: single view appearance

+0

共有ありがとう:) –

0

は、この方法を試してください。

  <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="@dimen/scale_10" 
       android:orientation="horizontal"> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/map_mark"/> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="@dimen/scale_10" 
        android:gravity="top" 
        android:text="TEXT HERE"/> 
      </LinearLayout> 
関連する問題