如何在 Android L 中为 CardView 小部件设置填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24896166/
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
How to set the padding for CardView widget in Android L
提问by nomongo
I'm using android:paddingLeft
and android:paddingTop
to set the padding for the new CardView
widget but it doesn't work.
我正在使用android:paddingLeft
和android:paddingTop
为新CardView
小部件设置填充,但它不起作用。
I can set the margin for all the controls inside the CardView
as a workaround but that's a pain if there are too many controls.
我可以为里面的所有控件设置边距CardView
作为一种解决方法,但是如果控件太多,那会很痛苦。
How to set padding for the new cardview widget?
如何为新的 cardview 小部件设置填充?
<android.support.v7.widget.CardView
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="200dp"
android:paddingLeft="20dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="20dp"
android:paddingBottom="@dimen/activity_vertical_margin"
card_view:cardCornerRadius="2dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</android.support.v7.widget.CardView>
采纳答案by alanv
CardView prior to L-preview uses RoundRectDrawableWithShadow
to draw its background, which overrides Drawable.getPadding()
to add shadow padding. The view background gets set via code after inflation, which overrides any padding specified in XML.
在 L-preview 之前的 CardViewRoundRectDrawableWithShadow
用于绘制其背景,它会覆盖Drawable.getPadding()
添加阴影填充。视图背景在膨胀后通过代码设置,它会覆盖 XML 中指定的任何填充。
You have two options:
您有两个选择:
- Set padding at run time using
View.setPadding()
and be careful to adjust for the shadows (but only prior to L-preview!). - Place everything inside a layout that specifies padding.
- 在运行时使用设置填充
View.setPadding()
并小心调整阴影(但仅在 L 预览之前!)。 - 将所有内容放在指定填充的布局中。
The latter option is safest.
后一种选择是最安全的。
<android.support.v7.widget.CardView
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="200dp"
card_view:cardCornerRadius="2dp">
<FrameLayout
android:paddingLeft="20dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="20dp"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</FrameLayout>
</android.support.v7.widget.CardView>
回答by yarian
CardView should handle this using the contentPadding
attributes it comes with:
CardView 应该使用contentPadding
它附带的属性来处理这个问题:
<android.support.v7.widget.CardView
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="200dp"
card_view:cardCornerRadius="2dp"
card_view:contentPaddingLeft="20dp"
card_view:contentPaddingRight="@dimen/activity_horizontal_margin"
card_view:contentPaddingTop="20dp"
card_view:contentPaddingBottom="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"/>
</android.support.v7.widget.CardView>
回答by Anonsage
If you want to use CardView padding on pre-L devices, and have it look the same on Lollipop+ devices, then you will need to use setUseCompatPadding(true)
, or the XML variant cardUseCompatPadding="true"
.
如果您想在 L 之前的设备上使用 CardView 填充,并让它在 Lollipop+ 设备上看起来相同,那么您将需要使用setUseCompatPadding(true)
, 或 XML 变体cardUseCompatPadding="true"
。
This is because "CardView adds additional padding to draw shadows on platforms before L."[1] So, the default implementation has the different API versions looking different and views may not line up properly. So, the easiest way to fix that issue is the ways stated above, or use margins instead.
这是因为“CardView 添加了额外的填充以在 L 之前在平台上绘制阴影。”[1] 因此,默认实现具有不同的 API 版本看起来不同,并且视图可能无法正确排列。因此,解决该问题的最简单方法是上述方法,或者改用边距。
Example Code
示例代码
<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:layout_width="match_parent"
android:layout_height="wrap_context"
card_view:cardUseCompatPadding="true"
card_view:contentPadding="8dp"
card_view:cardCornerRadius="4dp" >
...
</android.support.v7.widget.CardView>
Sources
来源
[1] CardView.setUseCompatPadding(boolean)
[1] CardView.setUseCompatPadding(boolean)
回答by HannahCarney
This is what worked for me - putting every item in a frame layout
这对我有用 - 将每个项目放在框架布局中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp">
<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:background="@color/white_three"
android:orientation="vertical"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="@dimen/card_elevation"
card_view:cardMaxElevation="0dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
</android.support.v7.widget.CardView>
Double check that card_view is "http://schemas.android.com/apk/res-auto" and not tools, and also set negative margins on the frame view to maintain shadows - works fine.
仔细检查 card_view 是“ http://schemas.android.com/apk/res-auto”而不是工具,并在框架视图上设置负边距以保持阴影 - 工作正常。