首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >CardView内部的CardView:删除额外的填充

CardView内部的CardView:删除额外的填充
EN

Stack Overflow用户
提问于 2016-02-24 03:03:25
回答 1查看 1.5K关注 0票数 0

我使用CardView的AppCompat支持库在CardView中实现了一个由CardView组成的布局。第一个布局是第一层,第二个布局位于第一层之上。在棒棒糖上一切都很好,但正如Android dev doc所说,为了在pre-L版本上创建阴影,添加了额外的填充。

以下是L版本:

和pre-L版本:

我已经尝试了很多其他帖子中的变通方法来删除这些额外的填充,但都不起作用。我可能漏掉了什么,但我不知道是什么。

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

代码语言:javascript
代码运行次数:0
运行
复制
<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版本上有很好的变通方法,我非常感兴趣!

EN

回答 1

Stack Overflow用户

发布于 2016-02-24 03:16:54

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

Similar question here.这里是一个例子,注意createviewholder中的"determineLayout()“方法。

代码语言:javascript
代码运行次数:0
运行
复制
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
    }
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35585984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档