2016-06-10 27 views
2

我要創建一個按鈕,如下圖:的Android - 巴頓與多個可繪製

Sample button

它幾乎沒關係,就像這樣:

<Button 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/main_shape_button" 
    android:fontFamily="sans-serif-condensed" 
    android:textColor="@color/colorSecundary" 
    android:textAlignment="viewStart" 
    android:drawableLeft="@drawable/ico_alerta" 
    android:drawableRight="@drawable/ico_seta_r" 
    android:text="@string/main_botao_painel_alertas" /> 

其中 「@繪製/ main_shape_button」是以下內容:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:left="-1dp" android:right="-1dp" android:bottom="-1dp"> 
     <shape> 
      <solid android:color="@android:color/transparent" /> 
      <stroke android:width="1dp" android:color="#ffffff" /> 
     </shape> 
    </item> 
</layer-list> 

但是,我還是要加上大喊流量指標與用戶的未查看警報的數量。

我怎樣才能實現呢?

更新:

我實現它擴展按鈕類:

public class AlertButton extends Button { 

    public AlertButton(Context context) { 
     super(context); 
    } 

    public AlertButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public AlertButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     int h = canvas.getHeight(); 
     int w = canvas.getWidth(); 
     //Calculate the x coordinate 
     float xCoodinate = (3*w)/4; 
     //Calculate 80% of the button height to the ratio. 
     float ratio = (h/2) * 0.8f; 

     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(getResources().getColor(R.color.colorTertiary)); 

     canvas.drawCircle(x, h/2 , ratio, paint); 

     Paint textPaint = new Paint(); 
     textPaint.setColor(Color.BLACK); 
     textPaint.setTextSize(50f); 
     textPaint.setAntiAlias(true); 
     textPaint.setTextAlign(Paint.Align.CENTER); 

     String text = "10"; 

     Rect bounds = new Rect(); 
     paint.getTextBounds(text, 0, text.length(), bounds); 

     float yCoordinate = 71f; //hard coded 
     canvas.drawText(text, x, yCoordinate, textPaint); 
    } 
} 

不過,我並沒有發現如何計算正在繪製文本的y座標。

我該怎麼做?

回答

2

而不是Button我會創建一個具有所需外觀的佈局,並使其可點擊android:clickable="true"屬性。

另一種選擇是通過擴展Button來創建您自己的自定義Component並覆蓋它的適當方法。

查看this link瞭解更多關於定製組件的信息。使用Android

0

添加屬性:點擊= 「真」

<Button 
    android:id="@+id/your_button" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/main_shape_button" 
    android:fontFamily="sans-serif-condensed" 
    android:textColor="@color/colorSecundary" 
    android:textAlignment="viewStart" 
    android:drawableLeft="@drawable/ico_alerta" 
    android:drawableRight="@drawable/ico_seta_r" 
    android:clickable="true" 
    android:text="@string/main_botao_painel_alertas" />