Element LinearLayout is not allowed in CardView

♀尐吖头ヾ 提交于 2019-12-10 03:14:19

问题


Note: PLEASE READ THE QUESTION before mark it as duplicate! I've mentioned that the similar question has not solved my problem.


I am using android.support.v7.widget.CardView for my layout xml. However when I place a LinearLayout within the CardView, Android Studio gives me the following error "Element LinearLayout is not allowed here".

In fact ANY types of widgets (like TextView) would cause this error, I have tried solutions from LinearLayout flagged if used in CardView link, which was to restart Android Studio. But it did not work. Below is my xml code:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout>

    </LinearLayout>

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

And I will also post a screenshot!

What could be the problem??


回答1:


So, a collection of suggestions, just to be sure:

  • restart AS
  • invalidating Caches & restart
  • Check if the right dependencies are added
  • clean & rebuild of project
  • try same code in another file/project
  • trying to find a backup of your project
  • maybe computer reboot



回答2:


Check if the Gradle dependency is added properly for the CardView.

In build.gradle

implementation 'com.android.support:cardview-v7:28.0.0'

And then in the XML

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#fff"
        app:cardCornerRadius="2dp"
        android:elevation="2dp">

    <TextView android:layout_width="match_parent"
           android:layout_height="wrap_content" android:id="@+id/textView" />

</androidx.cardview.widget.CardView>



回答3:


Just replace android.support.v7.widget.CardView to androidx.cardview.widget.CardView




回答4:


make sure that you did not inter constrains(magic stick) to card view because it will add constrains to card view and prevent any linear layout.



来源:https://stackoverflow.com/questions/33672739/element-linearlayout-is-not-allowed-in-cardview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!