我正在使用新的 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 中的片段中使用它。即使我在 CardView 上使用android:layout_marginLeft="18dp"
和android:layout_marginRight="18dp"
,该卡也会扩展整个屏幕宽度( match_parent )。
任何想法我可能做错了什么?
答案 0 :(得分: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>
但它通常不是最佳的。
答案 1 :(得分:17)
之前我遇到过同样的问题,我在这里找到答案...... CardView layout_width="match_parent" does not match parent RecyclerView width
在ViewPager中放置CardView xml时,将ViewGroup传递给它。这将允许inflater知道要引用的布局参数。
要使布局文件膨胀,请使用以下内容:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_list_item, viewGroup,false);
希望有所帮助
答案 2 :(得分:6)
ViewPager.LayoutParams不会扩展ViewGroup.MarginLayoutParams
就这么简单。
http://developer.android.com/reference/android/support/v4/view/ViewPager.LayoutParams.html
答案 3 :(得分:0)
在cardView中将 app:cardPreventCornerOverlap 属性设置为 false
<android.support.v7.widget.CardView
...
app:cardPreventCornerOverlap="false"
...
答案 4 :(得分:-1)
您需要将卡片视图放置在布局中,以便正确显示边距,尤其是当卡片视图在回收者视图中显示为项目时。
<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>