Android 缩小卡片视图项目之间的差距
我正在开发应用程序,我正在使用CardView和RecyclerView 我想减少两个卡片项目之间的空间。要指定cardview的宽度。(不希望将宽度设置为与\u父项匹配) 以下是我的密码- 我已经尝试过Android 缩小卡片视图项目之间的差距,android,android-recyclerview,android-cardview,Android,Android Recyclerview,Android Cardview,我正在开发应用程序,我正在使用CardView和RecyclerView 我想减少两个卡片项目之间的空间。要指定cardview的宽度。(不希望将宽度设置为与\u父项匹配) 以下是我的密码- 我已经尝试过卡片视图:cardMaxElevation=“1dp”,卡片视图:cardElevation=“1dp”和卡片视图:contentPadding=“-8dp”,但这对我不起作用 <android.support.v7.widget.RecyclerView android:
卡片视图:cardMaxElevation=“1dp”
,卡片视图:cardElevation=“1dp”
和卡片视图:contentPadding=“-8dp”
,但这对我不起作用
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_marginTop="15dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"/>
将两个项目用“LinearLayout”包装,使它们彼此相邻
<android.support.v7.widget.CardView
android:id="@+id/companyNameCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardBackgroundColor="@color/logo_yellow_light"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="8dp">
<LinearLayout
android:id="@+id/mainCardRelative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/full_transparent_color">
<RelativeLayout
android:id="@+id/thumbnailRelative"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:layout_marginStart="5dp"
android:background="@color/full_transparent_color">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:contentDescription="@string/app_name"
android:focusable="true"
android:src="@drawable/ic_job" />
</RelativeLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/thumbnailRelative"
android:background="@color/full_transparent_color"
android:gravity="start"
android:padding="5dp"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="13sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
您确实将CardView
的宽度设置为match\u parent
。只需将其设置为wrap\u content
共享所有适配器视图xml文件为什么不在Java文件中动态设置高度?@Vucko我尝试过wrap\u content,但它不起作用。。两个卡片项目之间的差距仍然很小same@TaslimOseni我试着用DisplayMetrics DisplayMetrics=newdisplaymetrics()设置它;this.activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);int height=displayMetrics.height像素/7;int-width=displayMetrics.widthPixels/2;RelativeLayout.LayoutParams LayoutParams=(RelativeLayout.LayoutParams)holder.card_view.getLayoutParams();layoutParams.height=高度;layoutParams.width=宽度;holder.card_view.setLayoutParams(layoutParams)代码>
<android.support.v7.widget.CardView
android:id="@+id/companyNameCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardBackgroundColor="@color/logo_yellow_light"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="8dp">
<LinearLayout
android:id="@+id/mainCardRelative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/full_transparent_color">
<RelativeLayout
android:id="@+id/thumbnailRelative"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:layout_marginStart="5dp"
android:background="@color/full_transparent_color">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:clickable="true"
android:contentDescription="@string/app_name"
android:focusable="true"
android:src="@drawable/ic_job" />
</RelativeLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/thumbnailRelative"
android:background="@color/full_transparent_color"
android:gravity="start"
android:padding="5dp"
android:text="@string/app_name"
android:textColor="@color/black"
android:textSize="13sp" />
</LinearLayout>
</android.support.v7.widget.CardView>