如何在文本视图的文本周围设置边框



我将首先解释一下,我想要获取的不是视图本身周围的边框,而是实际上它是文本视图中文本周围的边框。

我已经尝试在文本视图中设置阴影并设置样式.xml,但这些解决方案都不起作用。我试图使下面的代码正常工作,但我远不是一个好的开发人员,所以我甚至不知道如何使用它。我所知道的是它涉及反思(不是我知道这意味着什么(。

My java class (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>

my_layout.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,我需要什么才能使其正常工作?

编辑:

这就是我想做的,它实际上是在文本周围放置一个黑色边框: 点击这里查看图片

您可以使用阴影。下面的代码在文本周围创建黑色阴影。更改颜色 更改阴影颜色字段。阴影Dy是阴影的垂直偏移量和阴影Dx的水平偏移量。

android:shadowColor="#000000"
android:shadowRadius="5"
android:shadowDy="2"
android:shadowDx="2"

只需在 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
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>

希望对您有所帮助。根据需要在任意数量的文本视图中使用它。如果您想要其他东西,请发布图片。

最新更新