android - 在 TextView 元素的底部添加边框
android - add border at bottom of TextView element
如何在 Android 中的 TextView 底部添加边框。以下是我的TextView的XML代码
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Sales"
android:textColor="#000"
android:gravity="center"
android:padding="10dp"
/>
我正在给 XML 打电话,
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext,
R.layout.layoutName, lables);
如何在每个内容下方添加边框。
非常感谢任何帮助。
您可以像这样在文本视图下方放置一个视图:
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Sales"
android:textColor="#000"
android:gravity="center"
android:padding="10dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary"
/>
</LinearLayout>
如果您使用的是 recyclerview,则可以使用 DividerItemDecoration
。
添加以下代码:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
recyclerView.addItemDecoration(dividerItemDecoration);
使用 Suraj 的代码后,由于我使用的是数组适配器,我遇到了一个新错误“数组适配器要求资源 ID 为文本视图”。我在以下参考资料的帮助下修复了该错误 link.
"ArrayAdapter requires the resource ID to be a TextView" xml problems.
这是我的 java 代码,
ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(mContext, R.layout.home_callus_selection,R.id.text, lables);
和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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/opening_today_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
>
<ImageView
android:id="@+id/iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:paddingLeft="8dp"
android:layout_centerVertical="true"
android:src="@drawable/callred" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv"
android:textSize="20sp"
android:textColor="#000"
android:padding="10dp"
android:text="Sales" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#C1BFBF"
/>
</LinearLayout>
如何在 Android 中的 TextView 底部添加边框。以下是我的TextView的XML代码
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Sales"
android:textColor="#000"
android:gravity="center"
android:padding="10dp"
/>
我正在给 XML 打电话,
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(mContext,
R.layout.layoutName, lables);
如何在每个内容下方添加边框。
非常感谢任何帮助。
您可以像这样在文本视图下方放置一个视图:
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Sales"
android:textColor="#000"
android:gravity="center"
android:padding="10dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary"
/>
</LinearLayout>
如果您使用的是 recyclerview,则可以使用 DividerItemDecoration
。
添加以下代码:
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.HORIZONTAL);
recyclerView.addItemDecoration(dividerItemDecoration);
使用 Suraj 的代码后,由于我使用的是数组适配器,我遇到了一个新错误“数组适配器要求资源 ID 为文本视图”。我在以下参考资料的帮助下修复了该错误 link.
"ArrayAdapter requires the resource ID to be a TextView" xml problems.
这是我的 java 代码,
ArrayAdapter<String>dataAdapter = new ArrayAdapter<String>(mContext, R.layout.home_callus_selection,R.id.text, lables);
和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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/opening_today_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
>
<ImageView
android:id="@+id/iv"
android:layout_width="25dp"
android:layout_height="25dp"
android:paddingLeft="8dp"
android:layout_centerVertical="true"
android:src="@drawable/callred" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv"
android:textSize="20sp"
android:textColor="#000"
android:padding="10dp"
android:text="Sales" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#C1BFBF"
/>
</LinearLayout>