如何在具有圆角和透明背景的对话框中的回收视图中使用项目装饰
how to use item decoration in a recyclerview within a dialog with rounded corners and transparent background
我正在使用一个对话框,在这个对话框中我正在使用一个 recyclerview,我还为该对话框设置了一个带有圆角的自定义背景可绘制对象。
问题是每当我尝试为 recyclerview 设置项目装饰时,它会在对话框的底部切角显示一些奇怪的线条,就像这里 1 and here 2
我认为那是因为我将对话框背景设置为透明。
我想知道如何删除这些行
物品装饰
public ItemDecoration(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.recycler_horizontal_divider);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i <= childCount-2; 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);
}
}
对话框的背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"></solid>
<corners android:radius="@dimen/viewsRadius"></corners>
</shape>
对话框布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:background="@drawable/rounded_corners_dialog_back"
>
<TextView
android:id="@+id/dialogNameTxt"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:tag="bold"
android:text=""
android:textColor="@color/black"
android:textSize="@dimen/textSize"
android:textStyle="" />
<TextView
android:id="@+id/closeBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:tag="icon"
android:gravity="center"
android:background="@drawable/yellow_circle"
android:textColor="@color/white"
android:textSize="@dimen/textSize"
android:text="@string/closeIcon" />
<android.support.v7.widget.RecyclerView
android:id="@+id/dialogList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/closeBtn"
android:tag="bold" />
<Button
android:id="@+id/addBtn"
android:layout_width="match_parent"
android:layout_height="@dimen/viewsHeight"
android:text="@string/add"
android:tag="bold"
android:layout_margin="10dp"
android:textColor="@color/white"
android:textAllCaps="false"
android:background="@drawable/rounded_yellow_button"
android:layout_below="@id/dialogList"
/>
<android.support.constraint.ConstraintLayout
android:visibility="gone"
android:id="@+id/commentView"
android:layout_width="match_parent"
android:layout_below="@id/dialogList"
android:layout_margin="10dp"
android:layout_height="wrap_content">
<EditText
android:id="@+id/messageEditTxt"
android:layout_width="0dp"
android:layout_height="@dimen/viewsHeight"
android:background="@drawable/edit_text_back"
android:ems="10"
android:hint="@string/write_a_comment"
android:inputType="textPersonName"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:layout_marginEnd="10dp"
android:tag="regularFont"
android:textColor="@color/black"
android:textCursorDrawable="@null"
app:layout_constraintEnd_toStartOf="@+id/sendBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/sendBtn"
android:layout_width="@dimen/circleButtonDimension"
android:layout_height="@dimen/circleButtonDimension"
android:background="@drawable/yellow_circle"
android:gravity="center"
android:padding="@dimen/iconTextPadding"
android:tag="icon"
android:text="@string/sendIcon"
android:textColor="@color/white"
android:textSize="@dimen/iconTextSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dialogNameTxt"
android:id="@+id/cancelDialogView"
android:visibility="gone">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:tag="regularFont"
android:text="@string/confirm_cancel"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintBottom_toTopOf="@id/yesBtn"/>
<Button
android:id="@+id/yesBtn"
android:layout_width="0dp"
android:layout_height="@dimen/viewsHeight"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/rounded_yellow_button"
android:text="@string/yes"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/noBtn"
android:layout_width="0dp"
android:layout_height="@dimen/viewsHeight"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/rounded_yellow_button"
android:text="@string/no"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline4" />
<android.support.constraint.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/errorIcon"
android:textSize="60sp"
android:textColor="@color/red"
android:tag="icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
这里我将对话框的背景设置为透明以显示我的背景
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
使用这个
解决方案 1
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
DividerItemDecoration.VERTICAL));
解决方案 2
通过设置布局重力或在文本下方在项目布局底部声明这样的视图
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey"></View>
解决方法是为对话框背景添加 bottomPadding,使其与半径完全相同。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"></solid>
<corners android:radius="@dimen/viewsRadius"></corners>
<padding android:bottom="@dimen/viewsRadius"/>
</shape>
我正在使用一个对话框,在这个对话框中我正在使用一个 recyclerview,我还为该对话框设置了一个带有圆角的自定义背景可绘制对象。 问题是每当我尝试为 recyclerview 设置项目装饰时,它会在对话框的底部切角显示一些奇怪的线条,就像这里 1 and here 2 我认为那是因为我将对话框背景设置为透明。 我想知道如何删除这些行
物品装饰
public ItemDecoration(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.recycler_horizontal_divider);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();
int childCount = parent.getChildCount();
for (int i = 0; i <= childCount-2; 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);
}
}
对话框的背景
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"></solid>
<corners android:radius="@dimen/viewsRadius"></corners>
</shape>
对话框布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
android:background="@drawable/rounded_corners_dialog_back"
>
<TextView
android:id="@+id/dialogNameTxt"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:tag="bold"
android:text=""
android:textColor="@color/black"
android:textSize="@dimen/textSize"
android:textStyle="" />
<TextView
android:id="@+id/closeBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:tag="icon"
android:gravity="center"
android:background="@drawable/yellow_circle"
android:textColor="@color/white"
android:textSize="@dimen/textSize"
android:text="@string/closeIcon" />
<android.support.v7.widget.RecyclerView
android:id="@+id/dialogList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/closeBtn"
android:tag="bold" />
<Button
android:id="@+id/addBtn"
android:layout_width="match_parent"
android:layout_height="@dimen/viewsHeight"
android:text="@string/add"
android:tag="bold"
android:layout_margin="10dp"
android:textColor="@color/white"
android:textAllCaps="false"
android:background="@drawable/rounded_yellow_button"
android:layout_below="@id/dialogList"
/>
<android.support.constraint.ConstraintLayout
android:visibility="gone"
android:id="@+id/commentView"
android:layout_width="match_parent"
android:layout_below="@id/dialogList"
android:layout_margin="10dp"
android:layout_height="wrap_content">
<EditText
android:id="@+id/messageEditTxt"
android:layout_width="0dp"
android:layout_height="@dimen/viewsHeight"
android:background="@drawable/edit_text_back"
android:ems="10"
android:hint="@string/write_a_comment"
android:inputType="textPersonName"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:layout_marginEnd="10dp"
android:tag="regularFont"
android:textColor="@color/black"
android:textCursorDrawable="@null"
app:layout_constraintEnd_toStartOf="@+id/sendBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
<TextView
android:id="@+id/sendBtn"
android:layout_width="@dimen/circleButtonDimension"
android:layout_height="@dimen/circleButtonDimension"
android:background="@drawable/yellow_circle"
android:gravity="center"
android:padding="@dimen/iconTextPadding"
android:tag="icon"
android:text="@string/sendIcon"
android:textColor="@color/white"
android:textSize="@dimen/iconTextSize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/dialogNameTxt"
android:id="@+id/cancelDialogView"
android:visibility="gone">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
android:tag="regularFont"
android:text="@string/confirm_cancel"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintBottom_toTopOf="@id/yesBtn"/>
<Button
android:id="@+id/yesBtn"
android:layout_width="0dp"
android:layout_height="@dimen/viewsHeight"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/rounded_yellow_button"
android:text="@string/yes"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline4"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/noBtn"
android:layout_width="0dp"
android:layout_height="@dimen/viewsHeight"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/rounded_yellow_button"
android:text="@string/no"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline4" />
<android.support.constraint.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/errorIcon"
android:textSize="60sp"
android:textColor="@color/red"
android:tag="icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
这里我将对话框的背景设置为透明以显示我的背景
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
使用这个
解决方案 1
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
DividerItemDecoration.VERTICAL));
解决方案 2
通过设置布局重力或在文本下方在项目布局底部声明这样的视图
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey"></View>
解决方法是为对话框背景添加 bottomPadding,使其与半径完全相同。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"></solid>
<corners android:radius="@dimen/viewsRadius"></corners>
<padding android:bottom="@dimen/viewsRadius"/>
</shape>