将 CardView 添加到 FrameLayout 会忽略 CardView 的 XML 文件中定义的 LayoutParams

Adding a CardView to a FrameLayout ignores LayoutParams defined in the CardView's XML file

在我的 MainActivity 中,我使用 FloatingActionButton 以编程方式将 CardView 添加到现有容器(FrameLayout)。

但是当我添加 CardView 时,所有 LayoutParams(例如宽度、高度、边距等)都被忽略了。有没有办法使用 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>

activity_main.xml

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

最后是我用来添加 CardView 的代码:

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);
    }
}

要正确应用 XML 定义的 Viewlayout_* 属性,必须将保存它的 ViewGroup 提供给 LayoutInflater.在这种情况下,这意味着您需要将 container 作为 View.inflate() 调用中的最后一个参数传递。这样做也会导致膨胀的 View 自动添加到 ViewGroup,所以你不能用它调用 addView(),否则你会得到一个 IllegalStateException

此外,由于您将向 container 添加多个 ViewLinearLayout 似乎比 FrameLayout 更合适。我还要提一下,如果您计划添加很多额外的 ViewListViewRecyclerView 可能最终是更好的选择。