按钮的背景不显示

不显示背景。如果按钮没有背景,则显示 OK


<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="?attr/colorBackgroundFloating"

    tools:context=".MainActivity"

    tools:layout_editor_absoluteY="25dp">


    <Button

        android:id="@+id/button_1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:layout_weight="1"

        android:background="@drawable/my_round_button"

        android:onClick="onClick"

        android:text="@string/button_1"

        android:visibility="visible"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

当应用程序按钮没有背景时,按钮看起来像 ractangle


my_round_button


<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

    android:shape="ring">

    <solid android:color="#FFFFFF" />

</shape>

怎么了?按钮应该看起来像戒指


红糖糍粑
浏览 166回答 3
3回答

大话西游666

我建议您创建自定义视图,它以圆形的形式呈现视图,并根据我们从布局传递的宽度/高度。它更有活力。我确实喜欢Son Truong建议的答案,但这似乎不是更通用,因为我们需要硬编码尺寸宽度/高度。第一步:创建主题style.xml<declare-styleable name="CircleCompatTextView">&nbsp; &nbsp; <attr name="cctv_stroke_width" format="dimension" />&nbsp; &nbsp; <attr name="cctv_background_color" format="color" />&nbsp; &nbsp; <attr name="cctv_border_color" format="color" /></declare-styleable>第 2 步:创建自定义视图。public class CircleCompatTextView extends AppCompatTextView {&nbsp; &nbsp; private final Paint paintCircle = new Paint();&nbsp; &nbsp; private final Paint paintStroke = new Paint();&nbsp; &nbsp; private final Resources resources;&nbsp; &nbsp; private int strokeWidth;&nbsp; &nbsp; private int bgColor;&nbsp; &nbsp; private int borderColor;&nbsp; &nbsp; private int cxCy;&nbsp; &nbsp; public CircleCompatTextView(Context context, AttributeSet attrs) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, attrs);&nbsp; &nbsp; &nbsp; &nbsp; resources = context.getResources();&nbsp; &nbsp; &nbsp; &nbsp; TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleCompatTextView);&nbsp; &nbsp; &nbsp; &nbsp; strokeWidth = a.getDimensionPixelSize(R.styleable.CircleCompatTextView_cctv_stroke_width, 1);&nbsp; &nbsp; &nbsp; &nbsp; bgColor = a.getColor(R.styleable.CircleCompatTextView_cctv_background_color, resources.getColor(android.R.color.transparent));&nbsp; &nbsp; &nbsp; &nbsp; borderColor = a.getColor(R.styleable.CircleCompatTextView_cctv_border_color, resources.getColor(android.R.color.background_dark));&nbsp; &nbsp; &nbsp; &nbsp; a.recycle();&nbsp; &nbsp; &nbsp; &nbsp; init();&nbsp; &nbsp; }&nbsp; &nbsp; private void init() {&nbsp; &nbsp; &nbsp; &nbsp; paintCircle.setColor(bgColor);&nbsp; &nbsp; &nbsp; &nbsp; paintCircle.setFlags(Paint.ANTI_ALIAS_FLAG);&nbsp; &nbsp; &nbsp; &nbsp; paintStroke.setColor(borderColor);&nbsp; &nbsp; &nbsp; &nbsp; paintStroke.setStyle(Paint.Style.STROKE);&nbsp; &nbsp; &nbsp; &nbsp; paintStroke.setStrokeWidth(strokeWidth);&nbsp; &nbsp; &nbsp; &nbsp; paintStroke.setFlags(Paint.ANTI_ALIAS_FLAG);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void draw(Canvas canvas) {&nbsp; &nbsp; &nbsp; &nbsp; canvas.drawCircle(cxCy, cxCy, cxCy - strokeWidth / 2, paintStroke);&nbsp; &nbsp; &nbsp; &nbsp; canvas.drawCircle(cxCy, cxCy, cxCy - strokeWidth / 2, paintCircle);&nbsp; &nbsp; &nbsp; &nbsp; super.draw(canvas);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {&nbsp; &nbsp; &nbsp; &nbsp; super.onMeasure(widthMeasureSpec, heightMeasureSpec);&nbsp; &nbsp; &nbsp; &nbsp; int width = getMeasuredWidth();&nbsp; &nbsp; &nbsp; &nbsp; int height = getMeasuredHeight();&nbsp; &nbsp; &nbsp; &nbsp; int size = ((height > width) ? height : width);&nbsp; &nbsp; &nbsp; &nbsp; cxCy = size / 2;&nbsp; &nbsp; &nbsp; &nbsp; setMeasuredDimension(size, size);&nbsp; &nbsp; }}第三步:自定义视图的使用<CircleCompatTextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center|center_vertical"&nbsp; &nbsp; &nbsp; &nbsp; android:padding="16dp"&nbsp; &nbsp; &nbsp; &nbsp; android:textAlignment="center"&nbsp; &nbsp; &nbsp; &nbsp; app:cctv_background_color="@android:color/transparent"&nbsp; &nbsp; &nbsp; &nbsp; app:cctv_border_color="@android:color/background_dark"&nbsp; &nbsp; &nbsp; &nbsp; app:cctv_stroke_width="2dp" />这是输出
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java