gpt4 book ai didi

java - 如何在 TextView 的文本周围设置边框

转载 作者:行者123 更新时间:2023-11-30 05:00:55 25 4
gpt4 key购买 nike

我首先要解释的是,我想要获取的不是 View 本身的边框,而是 textview 中文本周围的边框。

我已经尝试在 textview 内和 style.xml 上设置阴影,但这些解决方案均无效。我试图使下面的代码有效,但我远不是一个好的开发人员,所以我什至不知道如何使用它。我所知道的是它涉及反射(我不知道那是什么意思)。

我的 java 类 (OutlineTextView)

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;

import com.example.detetiveinvestigativo.R;

import java.lang.reflect.Field;

public class OutlineTextView extends AppCompatTextView {
private Field colorField;
private int textColor;
private int outlineColor;

public OutlineTextView(Context context) {
this(context, null);
}

public OutlineTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}

public OutlineTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

try {
colorField = TextView.class.getDeclaredField("mCurTextColor");
colorField.setAccessible(true);

// If the reflection fails (which really shouldn't happen), we
// won't need the rest of this stuff, so we keep it in the try-catch

textColor = getTextColors().getDefaultColor();

// These can be changed to hard-coded default
// values if you don't need to use XML attributes

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OutlineTextView);
outlineColor = a.getColor(R.styleable.OutlineTextView_outlineColor, Color.TRANSPARENT);
setOutlineStrokeWidth(a.getDimensionPixelSize(R.styleable.OutlineTextView_outlineWidth, 0));
a.recycle();
}
catch (NoSuchFieldException e) {
// Optionally catch Exception and remove print after testing
e.printStackTrace();
colorField = null;
}
}

@Override
public void setTextColor(int color) {
// We want to track this ourselves
// The super call will invalidate()

textColor = color;
super.setTextColor(color);
}

public void setOutlineColor(int color) {
outlineColor = color;
invalidate();
}

public void setOutlineWidth(float width) {
setOutlineStrokeWidth(width);
invalidate();
}

private void setOutlineStrokeWidth(float width) {
getPaint().setStrokeWidth(2 * width + 1);
}

@Override
protected void onDraw(Canvas canvas) {
// If we couldn't get the Field, then we
// need to skip this, and just draw as usual

if (colorField != null) {
// Outline
setColorField(outlineColor);
getPaint().setStyle(Paint.Style.STROKE);
super.onDraw(canvas);

// Reset for text
setColorField(textColor);
getPaint().setStyle(Paint.Style.FILL);
}

super.onDraw(canvas);
}

private void setColorField(int color) {
// We did the null check in onDraw()
try {
colorField.setInt(this, color);
}
catch (IllegalAccessException | IllegalArgumentException e) {
// Optionally catch Exception and remove print after testing
e.printStackTrace();
}
}

// Optional saved state stuff

@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.textColor = textColor;
ss.outlineColor = outlineColor;
ss.outlineWidth = getPaint().getStrokeWidth();
return ss;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
textColor = ss.textColor;
outlineColor = ss.outlineColor;
getPaint().setStrokeWidth(ss.outlineWidth);
}

private static class SavedState extends BaseSavedState {
int textColor;
int outlineColor;
float outlineWidth;

SavedState(Parcelable superState) {
super(superState);
}

private SavedState(Parcel in) {
super(in);
textColor = in.readInt();
outlineColor = in.readInt();
outlineWidth = in.readFloat();
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(textColor);
out.writeInt(outlineColor);
out.writeFloat(outlineWidth);
}

public static final Parcelable.Creator<SavedState>
CREATOR = new Parcelable.Creator<SavedState>() {

public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}

属性.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="OutlineTextView" >
<attr name="outlineColor" format="color" />
<attr name="outlineWidth" format="dimension" />
</declare-styleable>
</resources>

我的布局.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".Interface.CharacterSelection.CharacterSelectionFragment"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:background="@drawable/wood_texture"
android:clickable="true"
android:focusable="false"
android:id="@+id/cs_parent_layout">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/cs_textview_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/select_characters"
android:textSize="25sp"
android:textColor="@color/white"
android:fontFamily="@font/joystix_monospace"
app:outlineColor="@color/black"
app:outlineWidth="4dp"
android:gravity="center"
android:layout_marginBottom="7dp"
android:clickable="false"/>
</androidx.appcompat.widget.LinearLayoutCompat>

如何使用这个类?如何将 AppCompatTextView 转换为 OutlineTextView?我需要什么才能让它工作?

编辑:

这就是我想要做的,它实际上是在文本周围放置一个黑色边框: click here to see the image

最佳答案

只需在您的 Drawable 文件夹中创建一个名为 box_border.xml 的 drawabale。

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

<stroke android:color="@color/navy"/>
<stroke android:width="1dp"/>
<corners android:radius="00dp"/>
</shape>

并将其设置为 TextView 的背景

<TextView
android:id="@+id/cs_textview_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/select_characters"
android:textSize="25sp"
android:textColor="@color/white"
android:background="@drawable/box_border"
/>
</androidx.appcompat.widget.LinearLayoutCompat>

希望对您有所帮助。根据需要在尽可能多的 TextView 中使用它。如果您想要其他东西,请张贴图片。

关于java - 如何在 TextView 的文本周围设置边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58253302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com