CardView layout_margin无效

Eri*_*ran 35 android android-viewpager android-cardview

我正在玩新的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>
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我在ViewPager中的片段中使用它.该卡扩展了屏幕的整个宽度(match_parent),即使我在CardView上使用android:layout_marginLeft ="18dp"和android:layout_marginRight ="18dp".

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

zon*_*nda 34

如果你使用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>
Run Code Online (Sandbox Code Playgroud)

但它通常不是最佳的.

  • AbsListView.LayoutParams不会扩展ViewGroup.MarginLayoutParams.http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html因此,边距不适用于直接子视图.不过,谢天谢地,RecyclerView.LayoutParams确实扩展了MLP. (3认同)
  • 看起来像一个丑陋的黑客,但对我有用。将执行此操作,直到我转换为“RecylerView”。 (2认同)

Nix*_*tel 19

之前我遇到过同样的问题,我在这里找到答案... 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);
Run Code Online (Sandbox Code Playgroud)

希望有所帮助


Eri*_*ran 6

ViewPager.LayoutParams不会扩展ViewGroup.MarginLayoutParams

就这么简单.

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