Java 将CardView添加到FrameLayout会忽略在CardView';s XML文件

Java 将CardView添加到FrameLayout会忽略在CardView';s XML文件,java,android,xml,layout,view,Java,Android,Xml,Layout,View,在我的主要活动中,我使用floatingAction按钮以编程方式将CardView添加到现有容器(框架布局) 但当我添加CardView时,所有布局参数(例如宽度、高度、边距等)都将被忽略。有没有办法使用XML文件中指定的参数 这是我的密码: card_view.xml <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:c

在我的主要活动中,我使用floatingAction按钮以编程方式将CardView添加到现有容器(框架布局)

但当我添加CardView时,所有布局参数(例如宽度、高度、边距等)都将被忽略。有没有办法使用XML文件中指定的参数

这是我的密码:

card_view.xml

<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/cardview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    card_view:cardCornerRadius="2dp"
    card_view:cardBackgroundColor="@color/cardViewBackground">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="example Text" />

</android.support.v7.widget.CardView>

要正确应用XML定义的
视图
layout*
属性,必须将保存该属性的
视图组
提供给
LayoutInflater
。在本例中,这意味着您需要将容器作为
视图.inflate()调用中的最后一个参数传递。执行此操作还将导致膨胀的
视图
自动添加到
视图组
,因此您不能使用它调用
addView()
,否则将得到
非法状态异常


此外,由于您将在
容器中添加多个
视图
,因此
线性布局
似乎比
框架布局
更合适。我还要提到,如果您计划添加大量额外的
视图
,则
列表视图
回收视图
可能会是一个更好的选择。

您需要传递根
视图组
,即您的
容器
,作为
充气()中的最后一个参数
调用要应用的参数。另外,您确定要为此使用
FrameLayout
?@MikeM。我会试试看,不,我不确定。。。哈哈,我对安卓系统的开发还比较陌生,这是我第一次尝试制作应用程序。您会推荐什么视图?我想您需要一个垂直方向的
线性布局
。哦,另外,如果传递
容器
视图组
,则不需要调用
addView()
。它将自动添加。@MikeM。啊,好的。。。甚至解决了我的另一个问题。现在我可以添加多次了!非常感谢。是的,我编辑了我的评论。当您将
视图组
传递到
充气()
时,您不想调用
addView()
。并确保您已经更改了XML元素、
容器的类型以及
findViewById()
上的强制转换。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

        <TextView
            android:id="@+id/textViewDate"
            android:text="@string/default_main_activity_msg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp"
            android:textAlignment="center"
            android:padding="16dp" />

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:clickable="true"
            android:src="@drawable/ic_fab_add"
            android:layout_gravity="end"
            android:onClick="addCard"
            android:visibility="gone"/>

    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="com.life_hacks.liftnotes.activity.FragmentDrawer"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>
public void addCard(View v){
    View cardView = View.inflate(getApplicationContext(), R.layout.card_view, null);
    FrameLayout container = (FrameLayout) findViewById(R.id.container_body);
    if(container != null){
        container.addView(card_view);
    }
}