简体   繁体   English

CardView 没有使用 LayoutInflater 正确充气

[英]CardView is not inflating properly using LayoutInflater

I want to add CardView programmatically.我想以编程方式添加 CardView。

Here is my Main Activity XML Layout (activity_main.xml)这是我的主要活动 XML 布局 (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/linearLayout1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical">
</LinearLayout>

Here is my CardViewTemplate (card_view_template.xml)这是我的 CardViewTemplate (card_view_template.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cardViewTemplate"
    android:layout_width="160dp"
    android:layout_height="190dp"
    android:layout_margin="10dp"
    android:clickable="true"
    android:foreground="?android:selectableItemBackground">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="This is a Card" />

</androidx.cardview.widget.CardView>

Here is my Java Code (MainActivity.java)这是我的 Java 代码(MainActivity.java)

LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = findViewById(R.id.linearLayout1);
inflater.inflate(R.layout.card_view_template, parent);

见输出

Everything works fine till here.直到这里一切正常。

Now I want to add Card at certain position in my activity_main.xml as I am using multiple CardViews, I want to add Cards at certain position.现在我想在我的activity_main.xml中的某个 position 添加卡片,因为我正在使用多个 CardViews,我想在某些 position 添加卡片。 Hence, instead of the above code, I tried this:因此,我尝试了以下代码,而不是上面的代码:

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.card_view_template, null);
ViewGroup parent = findViewById(R.id.linearLayout1);
parent.addView(view, 0);

But it does not inflate properly.但它不能正确充气。 Only the Text is visible, The Card does not seem to appear.只有文本可见,卡片似乎没有出现。 在此处查看输出

When dynamically adding views, we shouldn't inflate the View with null ViewGroup parent.当动态添加视图时,我们不应该使用 null ViewGroup父视图来膨胀View

In this View view = inflater.inflate(R.layout.card_view_template, null);在这个View view = inflater.inflate(R.layout.card_view_template, null); here parent is specified as null, this caused the problem.这里父被指定为null,这导致了问题。

Specify the parent to which the View will be attached to and only set attach to parent as false .指定View将附加到的父级,并且仅将附加到父级设置为false This way Parent will be specified but not attached.这样将指定但不附加父项。

Hence first declare parent (root) and then create View and specify the parent (root) and set attach to parent (root) false因此首先声明父(根)然后创建View并指定父(根)并将附加到父(根)设置为false

This is the correct statement View view = inflater.inflate(R.layout.card_view_template, parent, false);这是正确的说法View view = inflater.inflate(R.layout.card_view_template, parent, false);

Hence the complete code will be:因此完整的代码将是:

LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = findViewById(R.id.linearLayout1);
View view = inflater.inflate(R.layout.card_view_template, parent, false);
parent.addView(view, 0);

i suggest that you use directly the linear layout,like:我建议你直接使用线性布局,比如:

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.card_view_template, null);
LinearLayout LL = findViewById(R.id.linearLayout1);
LL.addView(view,0);

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

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