繁体   English   中英

CardView中的layout_margin无法正常工作

[英]layout_margin in CardView is not working properly

我正在使用新的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中使用它。 即使我在android:layout_marginLeft="18dp"使用android:layout_marginLeft="18dp"android:layout_marginRight="18dp"卡片也会扩展整个屏幕( match_parent )的整个宽度。

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

如果您使用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>

但这通常不是最佳选择。

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

在将ViewPager中的CardView xml膨胀时,将其中的ViewGroup传递给它。 这将使充气机知道要引用哪些布局参数。

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

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

希望能有所帮助

ViewPager.LayoutParams不扩展ViewGroup.MarginLayoutParams

就这么简单。

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

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

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

<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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM