<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" />
其中“@drawable/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坐标。
我该怎么做?
最佳答案
我将创建一个具有所需外观的布局,并通过Button
属性使其可单击,而不是android:clickable="true"
。
另一个选择是通过扩展Component
创建自己的自定义Button
,并覆盖它的适当方法。
查看this link了解有关自定义组件的更多信息。