2013-07-24 117 views
6

我知道在Android中我們可以使用函數setCompoundDrawables(..)添加drawable。添加繪製到EditText的左上角

我的問題是,我的EditText是multy-lines,我想把drawable放在EditText的左上角。但在setCompoundDrawables()函數中使用時,只能選擇將drawable放在特定的一側。

是否有可能在Android API上做到這一點?

+0

檢查這一個 - http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds%28int,%20int,%20int,%20int%29 – g00dy

+2

你可以用'setCompoundDrawablesWithIntrinsicBounds()'來代替'setCompoundDrawables(..)',就像so - >'EditTextId.setCompoundDrawablesWithIntrinsicBounds(0,myIcon,0,0);' – g00dy

+0

我可以在myIcon中放置什麼值?我知道這是int ..我可以選擇哪些值? – blay

回答

5

似乎是不可能的,只有EditText/TextView沒有anyside效果和額外的意見(我提供了最後的方式,但它需要更多的工作與圖像,可能不適用於每一部手機,因爲不同的編輯文本backgorunds取決於manufature)。同樣的問題已經被多次詢問:例如,herehere

按我的理解最簡單和fastests方式還不如不建議在上述問題(由RelativeLayout使用),但只用FrameLayout以下方式(你有2次VS使用RelativeLayout + ImageView 3視圖): 製作您的圖標9patch(使用9patch tool)。例如。你最初以下圖標:

original icon

,那麼你可以將其轉換爲下列之一:

enter image description here(你可以看到黑色像素,顯示面積與視圖和領域的內容被拉伸)

使用下面的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 --> 
     <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> 

給出以下結果(我文本添加多行):

result image

而且,它甚至有可能符合任務只有EditText(但你可能會失去默認的EditText背景,可能不是很好,但是你可以將其添加到您的寧 - 圖像;但它在所有手機上仍然是相同的,而不是原生之一)。

以下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> 
相關問題