CardView中的CardView:删除额外的填充

时间:2016-02-23 19:03:25

标签: android android-layout android-appcompat android-cardview appcompat-v7-r23

我使用适用于CardView的AppCompat支持库在CardView内部实现了由CardView组成的布局。第一个布局是第一层,第二个布局是第二层。在Lollipop上一切都很好,但正如Android dev doc所述,为了在前L版本上创建阴影,会添加额外的填充。

这是L版本:

L CardView version

和前L版:

Pre-L CardView version

我已尝试过其他帖子中的大量解决方法来删除这些额外的填充,但没有任何效果。我可能错过了一些东西,但我不知道是什么。

这是我的布局代码(我正在使用appcompat-v7 r23):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="80dp"
    android:id="@+id/adapter_line_favorites"
    android:clickable="false">

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        card_view:cardPreventCornerOverlap="false"
        card_view:contentPadding="0dp"
        card_view:cardCornerRadius="4dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="false"
            android:layout_alignParentRight="false"
            android:layout_alignParentEnd="false">

            <android.support.v7.widget.CardView
                xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                card_view:cardBackgroundColor="@color/CyanPrimaryDark"
                card_view:cardCornerRadius="4dp"
                android:id="@+id/line_layout">

                <FrameLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="70dp"
                        android:layout_gravity="center_horizontal">

                        <ImageView
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:id="@+id/line_icon"
                            android:layout_gravity="center"
                            android:layout_marginTop="15dp" />

                    </LinearLayout>

                    <ImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:id="@+id/dropdown"
                        android:layout_gravity="center_vertical|right"
                        android:layout_marginRight="20dp"
                        android:focusableInTouchMode="false"
                        android:src="@drawable/ic_action_keyboard_arrow_down" />
                </FrameLayout>

            </android.support.v7.widget.CardView>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/inner_favorite"
                android:visibility="gone">

            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout>

如果有人有一个很好的解决方案来处理L和pre-L版本,我就会感兴趣!

1 个答案:

答案 0 :(得分:1)

问题是您正在使用支持库中的CardView。此Cardview在pre-L(api21)上使用自己的填充/阴影机制。解决此问题的唯一方法是让适配器为pre-L设备使用不同的卡布局。

Similar question here.  这是一个示例,请注意createviewholder中的方法“determineLayout()”。

public class adapter_overview extends RecyclerView.Adapter<adapter_overview.AdapterViewHolder>{

public ArrayList<String> vehicleList = new ArrayList<>();
private View itemView;

public adapter_overview(ArrayList<String> vehicleList) {
    this.vehicleList.addAll(vehicleList.values());
}

@Override
public int getItemCount() {
    return vehicleList.size();
}

@Override
public AdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    itemView = LayoutInflater
            .from(viewGroup.getContext())
            .inflate(determineLayout(), viewGroup, 

    return new AdapterViewHolder(itemView);
}

@Override
public void onBindViewHolder(AdapterViewHolder adapterViewHolder, int i) {
    //do bind stuff
    }
}

public int determineLayout(){
        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT ){
            return R.layout.card_overview_v19;

        }else{
            return R.layout.card_overview;

    }
}

public static class AdapterViewHolder extends RecyclerView.ViewHolder{


    public AdapterViewHolder(View view) {
        super(view);

        //set up text view/etc
    }
}

}

相关问题