我要創建一個按鈕,如下圖:的Android - 巴頓與多個可繪製
它幾乎沒關係,就像這樣:
<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座標。
我該怎麼做?