2011-12-23 61 views
0

我想創建和盒子的EditText像這樣一個自定義的偏移背景: edittext prototypeEDITTEXT自定義偏移矩形背景

我應該如何去這樣做呢?

+0

您是否嘗試過在您的EditText中使用paddingLeft屬性? – 2014-06-24 17:53:01

回答

0

我會用包含邊框的LinearLayout作爲背景可繪製,頂部,左側和右側的填充以及底部的更高填充。

然後,根據您的背景爲您的EditText設置一個自定義,有狀態的9貼片可繪製。 實際上,如果您將背景修補爲9,則可以在LinearLayout和EditText中使用它。

此外,您不必使您的EditText可繪製有狀態;但我強烈建議你這樣做,這樣用戶實際上可以從你的EditText獲得視覺反饋。

樣品的編號:

佈局/ your_layout.xml

... 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/bg_border" 
    android:paddingLeft="10dip" 
    android:paddingRight="10dip" 
    android:paddingTop="10dip" 
    android:paddingBottom="30dip"> 


    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/bg_border_statelist" /> 

</LinearLayout> 
... 

抽拉/ bg_border_statelist.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/bg_border_selected" /> 
    <item android:state_focused="true" 
     android:drawable="@drawable/bg_border_selected" /> 
    <item android:drawable="@drawable/bg_border" /> 
</selector> 

假設你的背景圖像是9-貼片和稱爲bg_border.9.png ,並且相應的9補丁版本被稱爲bg_border_selected.9.png

乾杯!

+0

我想我從來沒有真正使用9貼片。我必須做任何特殊的事情才能使它成爲有狀態的嗎? – HighLife 2011-12-23 18:44:21

+0

您可以使用draw9patch工具製作圖像9補丁(http://developer.android.com/guide/developing/tools/draw9patch.html)。至於使其有狀態,請查看http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList :) – jcxavier 2011-12-23 18:45:12