安卓设置虚线分割线不显示
时间: 2023-11-27 13:03:47 浏览: 83
要在 Android 中设置虚线分割线,请使用 `android:divider` 属性和 `android:showDividers` 属性。以下是一个示例:
```xml
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/dashed_line"
android:dividerHeight="1dp"
android:showDividers="middle" />
```
在上面的示例中,我们将虚线分割线指定为 `dashed_line`,并将其高度设置为 `1dp`。我们还使用了 `android:showDividers="middle"` 属性,以指定在列表项之间绘制虚线分割线。
如果您想在 RecyclerView 中设置虚线分割线,可以使用 RecyclerView 的 `ItemDecoration` 类来实现。以下是一个示例:
```java
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public DividerItemDecoration(Context context) {
mDivider = ContextCompat.getDrawable(context, R.drawable.dashed_line);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
for (int i = 0; i < parent.getChildCount() - 1; i++) {
View child = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
```
在上面的示例中,我们创建了一个自定义的 `ItemDecoration` 类,并在 `onDrawOver` 方法中绘制虚线分割线。您可以将此类与 RecyclerView 关联以在 RecyclerView 中显示虚线分割线。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)