【问题标题】:The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)此组件的样式要求您的应用主题为 Theme.MaterialComponents(或后代)
【发布时间】:2020-01-24 14:17:12
【问题描述】:

我是 Android 新手,我可能有一个愚蠢/愚蠢的问题。我有一个活动,我想在其中动态创建多个输入字段。输入字段的数量由用户定义。

因为输入是样式化的并且由 2 个元素组成,并且不想每次都创建这些元素,因为元素每次都有多个相同的参数。这就是为什么我只为这两个元素创建了一个 XML 文件,我想在我的程序中使用它来创建输入。

View_input.xml

<?xml version="1.0" encoding="utf-8"?>
<merge 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayoutTableName"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="10dp"
        android:hint="@string/create_table_name"
        android:inputType="text"
        app:boxStrokeColor="@color/colorAccent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:placeholderText="@string/create_table_table_name_placeholder"
        app:startIconTintMode="src_in">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textInputName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:text="" />
    </com.google.android.material.textfield.TextInputLayout>
</merge>

我的目标 XML (activity_create_table_column_names.xml) 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout         
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.createtable.CreateTableColumnNamesActivity">

<LinearLayout
    android:id="@+id/columnNameInputContainer"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:orientation="vertical"
    android:padding="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/create_table_column_names"
    android:textStyle="bold" />

    <!-- HERE --> 
</LinearLayout>

我写&lt;!-- HERE --&gt;我希望我的所有输入字段都在哪里。

我一开始只显示一个输入:

private fun initLayout() {
    val container = findViewById<LinearLayout>(R.id.columnNameInputContainer)
    val inflater = applicationContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val view = inflater.inflate(R.layout.view_input, LinearLayout(this))
    container.addView(view)
}

但我得到以下异常:

Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:458)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:417)

我的 Styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
        ...
    </style>
</resources>

我做错了什么?这甚至是我应该编程这样的正确方法吗?

编辑Android.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="de.hsos.ma.adhocdb">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:appComponentFactory="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:appComponentFactory">
        <activity
            android:name=".ui.createtable.CreateTableColumnNamesActivity"
            android:parentActivityName=".ui.homescreen.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ui.createtable.CreateTableActivity"
            android:parentActivityName=".ui.homescreen.MainActivity" />
        <activity
            android:name=".ui.homescreen.TestTableActivity"
            android:parentActivityName=".ui.homescreen.MainActivity">

            <!--
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            -->
        </activity>
        <activity
            android:name=".ui.TableShow.TableShowActivity"
            android:parentActivityName=".ui.homescreen.MainActivity" />
        <activity android:name=".ui.tablelist.LayoutTableListItem" />
        <activity android:name=".ui.homescreen.MainActivity">

        </activity>
    </application>

</manifest>

【问题讨论】:

  • 您可以发布您的AndroidManifest.xml 内容
  • @MohammedAlaa 是的,我在帖子底部添加了清单

标签: android


【解决方案1】:

找到解决方案,从活动而不是应用程序中获取您的充气机,只需像这样修改您的initLayout()

// here is the fix
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

【讨论】:

  • 成功了,谢谢!!! @MohammedAlaa 如果我创建多个输入,您知道如何读取不同输入的文本(“@+id/textInputName”)吗?
  • 检查this是一样的,如果答案对你有帮助,你可以接受,这样当其他人搜索相同的问题时,他们可以找到解决方案
  • 感谢您的宝贵时间!我只是在创建视图时将所有文本视图添加到一个数组中,这对我有用!
  • 首先感谢您的宝贵时间。我将布局膨胀从 LayoutInflater.from(baseContext).inflate(it, null) 更改为 val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val inflatedView = inflater.inflate(it, null)
  • 我实际上只需要使用活动上下文而不是应用程序上下文创建我的视图。这个答案帮助我弄清楚了,谢谢。
【解决方案2】:

只需使用android:theme="@style/Theme.MaterialComponents.Bridge在芯片本身上设置主题:

<com.google.android.material.chip.Chip
    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:theme="@style/Theme.MaterialComponents.Bridge"****
    style="@style/CustomFiltersChipChoice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:chipStrokeColor="#F0F"
    app:chipStrokeWidth="1dp"
    app:textEndPadding="10dp"
    app:textStartPadding="10dp"
    tools:chipText="my chip"
    tools:text="Fortune" />

【讨论】:

    猜你喜欢
    • 2021-06-18
    • 2021-05-01
    • 2019-04-27
    • 2021-05-09
    • 1970-01-01
    • 2023-03-18
    • 2022-07-12
    相关资源
    最近更新 更多