2017-02-27 59 views
1

是否可以使用可繪製元素在其背景上添加填充到TextView中?或者我需要使用Draw 9-patch添加填充到帶有可繪製背景的textView

<TextView 
    android:layout_width="wrap_content" 
    android:layout_gravity="bottom|left" 
    android:layout_marginLeft="5dp" 
    android:layout_height="85dp" 
    android:background="@drawable/text_background" 
    android:id="@+id/counter" 
    android:textColor="@color/white" 
    android:textSize="12sp" 
    android:textStyle="bold" 
    android:gravity="center" 
    android:paddingLeft="2dp" 
    android:paddingRight="2dp" 
/> 

此填充設置不起作用。對於3-4位數字,背景可以正確顯示,但是應該調整(對於5/6位數字),它保持不變。
更新 - 添加圖像
enter image description here

+0

'android:drawablePadding'將在文本和可繪製文件之間創建一個填充間隙 –

+0

@Mohit Kacha不幸的是,這個參數不起作用 – Expiredmind

+0

你使用的是relativelayout?linearlayout? –

回答

0

如果要添加繪製作爲背景,然後drawablePadding不工作。

你只需要給你適當的填充TextView,以便它可以處理你的大部分用例。

<TextView 
    android:id="@+id/txt_bankid_login" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/layout_user_detail" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="@dimen/spacing_huge" 
    android:background="@drawable/textview_selector" 
    android:clickable="true" 
    android:textAllCaps="true" 
    android:padding="@dimen/spacing_small" 
    android:text="@string/login_bankid" 
    android:textColor="@color/app_background_color" 
    android:textSize="@dimen/font_normal" /> 

切勿爲高度使用靜態高度或寬度來佈局佈局。它會在不同的屏幕上產生問題,您將面臨文本截取。

+0

我沒有看到你的例子中的差異。設置textview的填充對背景可繪製尺寸沒有任何影響。 – Expiredmind

+0

是你的背景可繪製的XML或一些圖像?無論您的背景是可繪製的,您的佈局調整爲文本大小加上填充。而你的背景可繪製將覆蓋整個佈局大小。 –

+0

所以我需要將我的text_background.png添加到選擇器XML中而不添加任何參數,然後在TextView中將背景設置爲xml文件,然後填充將正確觸發? – Expiredmind

相關問題