Android - 具有多个可绘制对象的按钮

标签 android android-layout button

我必须创建一个按钮,如下图所示:

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" />

其中“@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 坐标。

我该怎么做?

最佳答案

我将创建一个具有所需外观的布局,并通过 android:clickable="true" 属性使其可点击,而不是使用 Button

另一种选择是通过扩展 Button 并覆盖其适当的方法来创建您自己的自定义 Component

检查this link有关自定义组件的更多信息。

关于Android - 具有多个可绘制对象的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37749234/

相关文章:

java - Activity onCreate 上出现 NullPointerException

java.lang.RuntimeException : Unable to start activity and skipping frames 错误

android - 抽屉没有覆盖操作栏

android - 带有 BottomNavigationView 的渐变状态栏

Javascript:使用按钮 Onclick 函数创建多个 Li 项目

python - Chaquopy:从 python 代码读取文本文件时出错 "No such file or directory"

java - setContentView 上的另一个 Resources$NotFoundException

android - Android 中的线性布局和相对布局

button - 如何设置按钮的大小Xamarin

javascript - 如何才能使输入字段为空时按钮不执行任何操作?