2014-11-14 27 views
2

在其他幾個帖子下面的建議,我實現了一個圓形按鈕,在應用程序中使用:Android的 - 如何定義配置的尺寸形狀

circular button

它是使用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或編程)配置?

感謝

+0

我想你可以在正確的尺寸縮放的TextView中設置文本,然後檢索尺寸,加大半徑尺寸並使用它? – 2014-11-14 14:21:36

+0

@ user3427079,「半徑大小的兩倍」 - 如何在不爲此特定按鈕創建其他xml的情況下執行此操作? – Vasiliy 2014-11-14 14:35:31

+0

textView.getWidth()* 2,類似於我想像的那樣 – 2014-11-14 14:44:11

回答

3

不能編程改變innerRadius屬性,因爲它只能從XML閱讀充氣時屬性(例如見this answer)。

但是,通過使用自定義可繪製來繪製環,您可以通過編程獲得或多或少相同的效果。例如:

public class RoundBorderDrawable extends GradientDrawable 
{ 
    private static final int RING_THICKNESS = 20; 
    private static final int PADDING = 8; 
    private static final int NORMAL_COLOR = Color.BLUE; 
    private static final int PRESSED_COLOR = Color.RED; 

    private Paint mPaint; 

    public RoundBorderDrawable() 
    { 
     mPaint = new Paint(); 
     mPaint.setStyle(Style.STROKE); 
     mPaint.setStrokeWidth(RING_THICKNESS); 
     mPaint.setColor(NORMAL_COLOR); 
    } 

    @Override 
    public boolean isStateful() 
    { 
     return true; 
    } 

    @Override 
    protected boolean onStateChange(int[] stateSet) 
    { 
     boolean result = super.onStateChange(stateSet); 

     int color = NORMAL_COLOR; 
     for (int i = 0; i < stateSet.length; i++) 
     { 
      if (stateSet[i] == android.R.attr.state_pressed) 
       color = PRESSED_COLOR; 
     } 

     if (color != mPaint.getColor()) 
     { 
      mPaint.setColor(color); 
      invalidateSelf(); 
      result = true; 
     } 

     return result; 
    } 

    @Override 
    public void draw(Canvas canvas) 
    { 
     super.draw(canvas); 
     int diameter = Math.min(canvas.getWidth(), canvas.getHeight()) - RING_THICKNESS - 2 * PADDING; 
     canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, diameter/2, mPaint); 
    } 
} 

這將引起該Button尺寸適合內圓(我以爲你想連畫一個圓非方的按鈕)。

然後只需設置一個RoundBorderDrawable的新實例作爲你的按鈕的背景(在這個例子中常量的屬性當然可以通過構造函數或setter方法提供)。

+0

感謝您的意見。我現在無法測試您的解決方法,但它現在似乎是最有前途的方法。 – Vasiliy 2015-01-20 09:57:40

+0

@Vasiliy我們在我們的項目中一直在使用這種類型的東西;希望它有用。 – matiash 2015-01-20 17:21:16

0

我有一個更好的方法來做到這一點。而不是Ring使用Oval和賦予行程與顏色白色和寬度@dimen/big_ring_button_thickness 但這不給你圓形。要實現圓形形狀,您的按鈕應該是方形的,我的意思是按鈕的寬度和高度應該相同。

相關問題