gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:16:09 24 4
gpt4 key购买 nike

在我的 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

此外,由于您将向您的容器添加多个View,因此LinearLayout似乎比一个 FrameLayout。我还要提到的是,如果您计划添加大量额外的 ViewListViewRecyclerView 最终可能是更好的选择.

关于java - 将 CardView 添加到 FrameLayout 会忽略 CardView 的 XML 文件中定义的 LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37848342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com