Android 按钮或 TextView 边框以编程方式,而不使用 setBackgroundDrawable 方法

问题描述 投票:0回答:3

我正在寻找一种以编程方式为文本视图或按钮添加边框而不使用 setBackgroundResource 方法的方法。

我在这里试图实现的目标是动态更改背景颜色但具有固定边框。 当我对背景边框使用 setBackgroundResource 方法时,以编程方式更改背景颜色后,边框不会保留。

android button border textview
3个回答
61
投票

简单的例子如何实现这一点:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java

package com.exmple.test;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
        gd.setCornerRadius(5);
        gd.setStroke(1, 0xFF000000);
        TextView tv = (TextView)findViewById(R.id.textView1);
        tv.setBackground(gd);


    }

}

0
投票

我想这会对你有帮助。

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <gradient android:startColor="#FFFFFF" 
        android:endColor="#FFFFFF"
        android:angle="270" />
      <corners android:radius="3dp" />
      <stroke android:width="5px" android:color="#eecc68" />
    </shape>
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="52dp"
        android:layout_marginTop="39dp"
        android:background="@drawable/button"
        android:text="Button" />

0
投票

我为此开设了这门课:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class BorderColorDrawable extends Drawable {


    private int mBorderLeft=0;
    private int mBorderTop=0;
    private int mBorderRight=0;
    private int mBorderBottom=0;
    private int mBorderColor = Color.BLACK;
    private int mFillColor = Color.TRANSPARENT;
    private Paint mBorderPaint = new Paint();

    public BorderColorDrawable() {

    }

    public void setBorderWidth(int left,int top,int right,int bottom) {
        mBorderLeft = left;
        mBorderTop = top;
        mBorderRight = right;
        mBorderBottom = bottom;
        invalidateSelf();

    }
    public void setBorderWidth(int width) {
        mBorderLeft = width;
        mBorderTop = width;
        mBorderRight = width;
        mBorderBottom = width;
        invalidateSelf();
    }
    public void setBorderColor(int color) {
        mBorderColor = color;
        mBorderPaint.setColor(color);
        invalidateSelf();
    }


    public void setFillColor(int color) {
        mFillColor = color;
        invalidateSelf();
    }



    @Override
    public void draw(@NonNull Canvas canvas) {
        Log.i("ramdev","draw " + mBorderLeft);
        if (mFillColor!=Color.TRANSPARENT)
            canvas.drawColor(mFillColor);
        Rect b = getBounds();
        if (mBorderLeft>0) {
            canvas.drawRect(0,0,mBorderLeft,b.bottom,mBorderPaint);
        }
        if (mBorderTop>0) {
            canvas.drawRect(0,0,b.right,mBorderTop,mBorderPaint);
        }
        if (mBorderRight>0) {
            canvas.drawRect(b.right-mBorderRight,0,b.right,b.bottom,mBorderPaint);
        }
        if (mBorderBottom>0) {
            canvas.drawRect(0,b.bottom-mBorderBottom,b.right,b.bottom,mBorderPaint);
        }
    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }


}

用途:

BorderColorDrawable background = new BorderColorDrawable()
// border left, top, right bottom in pixel! - convert from dp if needed
borderColorDrawable.setBorderWidth(3,3,3,3);
borderColorDrawable.setBorderColor(Color.BLACK);
borderColorDrawable.setFillColor(Color.WHITE);
© www.soinside.com 2019 - 2024. All rights reserved.