我正在尝试向 Android 上的 CustomTextView 添加可绘制背景。我尝试在styles.xml 中使用style 属性,但我无法应用它。到目前为止,这是我的实现。
自定义文本视图.java
public class CustomTextView extends android.support.v7.widget.AppCompatTextView {
public CustomTextView(Context context) {
super(context);
Typeface face = Typeface.createFromAsset(context.getAssets(), "font/Montserrat-Regular.ttf");
context.getDrawable(R.drawable.tv_bottom_line_only);
this.setTypeface(face);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
context.getDrawable(R.drawable.tv_bottom_line_only);
Typeface face = Typeface.createFromAsset(context.getAssets(), "font/Montserrat-Regular.ttf");
this.setTypeface(face);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face = Typeface.createFromAsset(context.getAssets(), "font/Montserrat-Regular.ttf");
context.getDrawable(R.drawable.tv_bottom_line_only);
this.setTypeface(face);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
我的可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="1dp"
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape android:shape="rectangle">
<stroke
android:width="0.5dp"
android:color="@android:color/black" />
</shape>
</item>
这是我在布局 xml 中的实现。
...
<com.project.utils.CustomTextView
android:id="@+id/profile_et_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="text"
android:padding="@dimen/_4sdp"
android:textColor="@color/black"
android:textSize="@dimen/_8sdp" />
这是问题的快照。我想删除出现在顶部的边框。
谢谢
要将可绘制对象设置为背景,您应该使用:
setBackground(context.getDrawable(R.drawable.tv_bottom_line_only));
现在底部只有一条线,您应该rectangle
从可绘制对象中删除该形状。
主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<org.mayday.myapplication.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>
tv_bottom_line_only xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="-8dp" android:left="-8dp" android:right="-8dp">
<shape>
<stroke android:width="0.5dp" android:color="@android:color/black" />
</shape>
</item>
</layer-list>
结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句