Android 将“背景”属性添加到自定义TextView组件,该组件底部只有边框
我试图在android上为CustomTextView添加一个可绘制的背景。我尝试在styles.xml中使用style属性,但无法应用它。 这是我到目前为止的实现 CustomTextView.javaAndroid 将“背景”属性添加到自定义TextView组件,该组件底部只有边框,android,android-layout,android-custom-view,androiddesignsupport,android-designer,Android,Android Layout,Android Custom View,Androiddesignsupport,Android Designer,我试图在android上为CustomTextView添加一个可绘制的背景。我尝试在styles.xml中使用style属性,但无法应用它。 这是我到目前为止的实现 CustomTextView.java public class CustomTextView extends android.support.v7.widget.AppCompatTextView { public CustomTextView(Context context) { super(context);
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));
现在,要在底部只有一条线,您应该从绘图表中删除矩形
形状
主要活动:
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>
电视\u底线\u仅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>
结果:
您需要实际将其设置为背景:
setBackground(context.getDrawable(R.drawable.tv_bottom_line_))代码>@MikeM。谢谢你的解决方案。它起了部分作用。现在的问题是,这条线出现在顶部和底部。我只需要在底部的行。您可以在我更新的帖子中查看我在布局中的实现。谢谢您的解决方案。但是,在这个解决方案的四个方面都看不到边界。这应该是微不足道的。为了得到底线,我们只需要不给它填充,这样它就不会隐藏在我们的文本视图下。你能用最新的代码更新你的问题吗?发布更新。请检查并帮助我纠正我的错误。我还尝试删除xml中的填充,但不起作用。感谢you@RakshitSorathiya例如,您是否尝试过删除形状矩形并将项目顶部减少到-8dp?检查我的电话号码是的。事实上我忘了更新。但是我试过了。在这里,它是,我删除底部根据您的帖子,文本视图正在变得透明。