在其他幾個帖子下面的建議,我實現了一個圓形按鈕,在應用程序中使用:Android的 - 如何定義配置的尺寸形狀
:它是使用XML選擇實施
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states
-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false"
android:drawable="@drawable/big_ring_button_unfocused" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false"
android:drawable="@drawable/big_ring_button_unfocused" />
<!-- Focused states
-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false"
android:drawable="@drawable/big_ring_button_focused" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false"
android:drawable="@drawable/big_ring_button_focused" />
<!-- Pressed
-->
<item android:state_pressed="true" android:drawable="@drawable/big_ring_button_pressed" />
</selector>
的..._聚焦文件的內容(其他人只是改變顏色):
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="@dimen/big_ring_button_inner_radius"
android:thickness="@dimen/big_ring_button_thickness"
android:useLevel="false">
<solid android:color="@color/white" />
</shape>
我想爲我的應用中的所有圓角按鈕使用此模板,但由於按鈕內部的文本發生更改,按鈕本身的大小應該改變。
我認爲我也許可以以編程方式做到這一點,所以我檢查的GradientDrawable的文檔 - 有沒有方法有改變innerRadius
屬性。
目前我每個圓形按鈕(選擇器,未聚焦,聚焦和按下)有4個XML文件,這是非常醜陋的,並將成爲一個頸部疼痛的維護。
我怎樣才能使這個按鈕的大小(XML或編程)配置?
感謝
我想你可以在正確的尺寸縮放的TextView中設置文本,然後檢索尺寸,加大半徑尺寸並使用它? – 2014-11-14 14:21:36
@ user3427079,「半徑大小的兩倍」 - 如何在不爲此特定按鈕創建其他xml的情況下執行此操作? – Vasiliy 2014-11-14 14:35:31
textView.getWidth()* 2,類似於我想像的那樣 – 2014-11-14 14:44:11