我有一个包含recyclerView的自定义对话框。我使用布局文件来查看我的recyclerview。当我在此视图中填充数据时,分隔符不存在且项目更靠近在一起。当我使用相同的布局文件来处理片段中的recyclerView时,它按预期工作。在两种情况下,recyclerView布局文件和项目布局文件都相同。我正在附加scren镜头以澄清事情
这些是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/explore_recyclerView"
android:paddingTop="1dp"
></android.support.v7.widget.RecyclerView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="3dp"
android:paddingBottom="1dp"
>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/post_card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="1dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arial New"
android:textSize="16dp"
android:id="@+id/preview_font_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monotype Solutions"
android:textSize="12dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/preview_imageview"
android:layout_gravity="left"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_star_outline_24dp"
android:background="@android:color/transparent"
android:tint="@android:color/holo_orange_light"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Favourite"
android:textSize="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center"
android:id="@+id/font_view_preview_layout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_visibility_24dp"
android:background="@android:color/transparent"
android:tint="@android:color/holo_green_dark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preview"
android:textSize="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center"
android:id="@+id/preview_buy_layout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_shopping_cart_24dp"
android:background="@android:color/transparent"
android:tint="@android:color/holo_blue_dark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Buy Now"
android:textSize="12dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="10dp"
android:gravity="center"
android:id="@+id/preview_similarity_layout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_shopping_cart_24dp"
android:background="@android:color/transparent"
android:tint="@android:color/holo_blue_dark"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Similar Fonts"
android:textSize="12dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_explore_view, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.explore_recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
}
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_explore_view);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.explore_recyclerView);
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
}
答案 0 :(得分:-1)
对于sizeof
中的分隔符,您必须使用ItemDecoration。
创建RecyclerView
,如下所示:
DividerItemDecoration.java
在drawable中有一个public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public DividerItemDecoration(Context context) {
mDivider = context.getResources().getDrawable(R.drawable.line_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; 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);
}
}
}
形状,如下所示:
line_divider.xml
最后,在<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="@color/colorTextHint" />
</shape>
:
colors.xml
最后,使用<color name="colorTextHint">#9f9f9f</color>
作为:
DividerItemDecoration.java
答案 1 :(得分:-2)
尝试在&#39;项目布局文件中使用此代码&#39;
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#D3D3D3"
/>
答案 2 :(得分:-3)
在android.support.v7.widget.CardView
app:cardElevation="2dp"
card_view:cardUseCompatPadding="true"