我使用CardView的AppCompat支持库在CardView
中实现了一个由CardView
组成的布局。第一个布局是第一层,第二个布局位于第一层之上。在棒棒糖上一切都很好,但正如Android dev doc所说,为了在pre-L版本上创建阴影,添加了额外的填充。
以下是L版本:
和pre-L版本:
我已经尝试了很多其他帖子中的变通方法来删除这些额外的填充,但都不起作用。我可能漏掉了什么,但我不知道是什么。
这是我的布局代码(我使用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版本上有很好的变通方法,我非常感兴趣!
发布于 2016-02-23 19:16:54
问题是您使用的是support库中的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
}
}
}
https://stackoverflow.com/questions/35585984
复制相似问题