//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout//布局方式(实质是容器,插件必须放在容器中)
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"(宽度适应父类)
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.myapplication.MyTextView
android:id="@+id/tv_one"//设置id名,在java中调用
android:text="@string/lalala"//text内容,如果java中重新设置这里的会被覆盖
android:textColor="@color/colorAccent"//文字颜色,调用color.xml中的设置
android:textStyle="bold"//字体:加粗、斜体、无样式三种,ctrl查看详情
android:textSize="30sp"//字号,单位是sp
android:background="@color/colorPrimary"//背景色,调用color.xml中的设置
android:gravity="center"//文本位置,居中,ctrl查看详情
android:shadowColor="@color/red"//阴影颜色
android:shadowRadius="3.0"//固定使用3.0
android:shadowDx="10.0"//水平倾斜
android:shadowDy="10.0"//垂直倾斜
android:singleLine="true"//单行显示
android:ellipsize="marquee"//跑马灯设置
android:marqueeRepeatLimit="marquee_forever"//永远循环显示
android:focusable="true"//焦点获取
android:focusableInTouchMode="true"//焦点获取
android:layout_width="match_parent"//宽度适应父类
android:layout_height="200dp" //TextView高度,单位是dp
/>
</LinearLayout>
//MyTextView.java
package com.example.myapplication;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class MyTextView extends androidx.appcompat.widget.AppCompatTextView {
public MyTextView(Context context) {
super(context);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused() {
return true;//设置焦点获取
}
}
效果图: