在 Android ListView 或 RecyclerView 中加入外边框
时间: 2023-08-29 18:04:29 浏览: 300
在 Android ListView 或 RecyclerView 中加入外边框可以通过以下几种方式实现:
1. 使用布局文件中的 `android:background` 属性来设置边框背景,例如:
```xml
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border" />
```
其中 `@drawable/border` 是一个自定义的边框背景,可以通过定义一个 XML 文件来实现。
2. 在代码中为 ListView 或 RecyclerView 设置背景,例如:
```java
listView.setBackground(ContextCompat.getDrawable(context, R.drawable.border));
```
其中 `R.drawable.border` 是一个自定义的边框背景的资源 ID。
3. 使用 CardView 或其他第三方 UI 库来实现边框效果,例如:
```xml
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
```
其中 CardView 是 Android Support Library 中提供的一个 UI 控件,可以实现圆角、阴影、边框等效果。
以上是一些常用的在 Android ListView 或 RecyclerView 中加入外边框的方式,可以根据具体需求选择合适的方式来实现。
阅读全文
相关推荐


















