自定义TextView
- 新建文件MyTextView,集成TextView
package com.pha.first.component;
import android.content.Context;
import android.util.AttributeSet;
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;
}
}
引用自定义TextView
- Layout布局文件中,将TextView改成自定义的TextView。引用his使用全路径com.pha.first.component.MyTextView
<LinearLayout 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"
tools:context=".MainActivity">
<com.pha.first.component.MyTextView
android:id="@+id/tv_one"
android:text="@string/tv_one"
android:textColor="@color/black"
android:textStyle="italic"
android:textSize="30sp"
android:background="@color/lightgray"
android:gravity="center"
/>
</LinearLayout>