38

我正在使用新的CardView,但边距似乎并不适用。

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#FFA"
card_view:layout_margind="4dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<TextView
    android:id="@+id/info_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

</LinearLayout>

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

顺便说一句,我在ViewPager中的Fragment中使用它。即使我在CardView上使用和,卡片也会扩展屏幕的整个宽度(match_parent ) 。android:layout_marginLeft="18dp"android:layout_marginRight="18dp"

有什么想法我可能做错了吗?

4

5 回答 5

37

如果你使用RecyclerView添加CardView,android:layout_margin应该足够了。

但是使用 ListView 来添加 CardView,你可能会这样做:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="11dp"
    android:layout_marginRight="11dp"
    android:layout_marginTop="11dp">
       (other layout ...)
</android.support.v7.widget.CardView>

</FrameLayout>

但它通常不是最优的。

于 2014-08-29T03:52:48.670 回答
23

我之前遇到过同样的问题,我在这里找到了答案... CardView layout_width="match_parent" 与父 RecyclerView 宽度不匹配

在您的 ViewPager 中膨胀 CardView xml 时,将 ViewGroup 传递到其中。这将允许inflater 知道要引用哪些布局参数。

要膨胀布局文件,请使用以下内容:

View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_list_item, viewGroup,false);

希望有帮助

于 2014-07-08T06:02:31.730 回答
7

ViewPager.LayoutParams 不扩展 ViewGroup.MarginLayoutParams

就是这么简单。

http://developer.android.com/reference/android/support/v4/view/ViewPager.LayoutParams.html

于 2015-02-28T09:59:52.143 回答
2

您需要将卡片视图放入布局中以正确显示边距,尤其是当卡片视图在回收站视图中显示为项目时。

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">

<androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardElevation="2dp"
        android:layout_marginBottom="25dp"/>
</LinearLayout>
于 2019-08-04T10:23:51.553 回答
0

在 cardView 中将 app:cardPreventCornerOverlap属性设置为false <android.support.v7.widget.CardView ... app:cardPreventCornerOverlap="false" ...

于 2018-05-23T18:54:11.280 回答