Android CardView 中的 layout_margin 无法正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24564233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
layout_margin in CardView is not working properly
提问by Eric Cochran
I am playing with the new CardView, but the margins don't seem to be applying.
我正在使用新的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>
By the way, I'm using it in a Fragmentthat is in a ViewPager. The card extends the entire width of the screen (match_parent) even though I am using android:layout_marginLeft="18dp"
and android:layout_marginRight="18dp"
on the CardView.
顺便说一句,我在ViewPager中的Fragment中使用它。即使我在CardView上使用和,卡片也会扩展屏幕的整个宽度(match_parent)。android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
Any ideas what I might be doing wrong?
任何想法我可能做错了什么?
采纳答案by Eric Cochran
ViewPager.LayoutParams does not extend ViewGroup.MarginLayoutParams
ViewPager.LayoutParams 不扩展 ViewGroup.MarginLayoutParams
It's that simple.
就这么简单。
http://developer.android.com/reference/android/support/v4/view/ViewPager.LayoutParams.html
http://developer.android.com/reference/android/support/v4/view/ViewPager.LayoutParams.html
回答by zonda
If you use RecyclerView to add CardView, android:layout_margin should be sufficient.
如果使用 RecyclerView 添加 CardView, android:layout_margin 应该就够了。
But using ListView to add CardView, you might do this:
但是使用 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>
But it is usually not the optimal one.
但它通常不是最佳的。
回答by Nixit Patel
I had the same issues before, I found answer in here... CardView layout_width="match_parent" does not match parent RecyclerView width
我之前遇到过同样的问题,我在这里找到了答案... CardView layout_width="match_parent" 与父 RecyclerView 宽度不匹配
At the time of inflating the CardView xml inside your ViewPager, pass the ViewGroup in it. this will allow inflater to know which layout parameters to refer.
在您的 ViewPager 中对 CardView xml 进行膨胀时,将 ViewGroup 传入其中。这将允许 inflater 知道要引用哪些布局参数。
To inflate the layout file use something like below:
要膨胀布局文件,请使用以下内容:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.view_list_item, viewGroup,false);
Hope that helps
希望有帮助
回答by Jorge Arindia
Set app:cardPreventCornerOverlapproperty to falsein cardView
<android.support.v7.widget.CardView
...
app:cardPreventCornerOverlap="false"
...
在 cardView 中将
app:cardPreventCornerOverlap属性设置为false<android.support.v7.widget.CardView
...
app:cardPreventCornerOverlap="false"
...
回答by Abdulmajeed Alyafey
You need to put your card view inside a layout in order to properly show margins, especially if the card view is shown as an item in a recycler view.
您需要将卡片视图放在布局中以正确显示边距,尤其是当卡片视图在回收器视图中显示为项目时。
<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>