有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在安卓 api 17中使用应用程序上下文膨胀视图时,java不能在安卓自定义toast的布局中使用material主题属性

我想要一个与任何视图上下文(如ActivityT上下文或片段上下文)无关的自定义toast。因此,我使用应用程序上下文来扩展它的自定义布局。在自定义toast布局中使用主题属性时,我遇到了一个奇怪的问题。这是我祝酒词的布局:

<?xml version="1.0" encoding="utf-8"?>
<安卓x.cardview.widget.CardView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_gravity="center"
    安卓:layout_marginTop="8dp"
    app:cardBackgroundColor="#ffffff"
    app:cardCornerRadius="8dp"
    安卓:theme="@style/AppTheme"
    app:cardElevation="@dimen/cardview_default_elevation">

    <LinearLayout
        安卓:id="@+id/content"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:gravity="center_vertical"
        安卓:orientation="horizontal"
        安卓:paddingStart="8dp"
        安卓:paddingTop="8dp"
        安卓:paddingEnd="8dp"
        安卓:paddingBottom="8dp">

        <TextView
            安卓:id="@+id/txtMessage"
            安卓:layout_width="0dp"
            安卓:layout_height="wrap_content"
            安卓:layout_weight="1"
            安卓:ellipsize="end"
            安卓:maxLines="1"
            安卓:textColor="?attr/colorPrimary"
            />

        <ImageView
            安卓:id="@+id/imgIcon"
            安卓:layout_width="24dp"
            安卓:layout_height="24dp"
            app:tint="?attr/colorPrimary" />

    </LinearLayout>

</安卓x.cardview.widget.CardView>

这就是我的风格。xml看起来像:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

这是我的主要活动:

import 安卓.os.Bundle
import 安卓.view.Gravity
import 安卓.view.LayoutInflater
import 安卓.widget.ImageView
import 安卓.widget.TextView
import 安卓.widget.Toast
import 安卓x.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<ImageView>(R.id.img).setOnClickListener {
            val toast = Toast(this@MainActivity.applicationContext)
            val view = LayoutInflater.from(this@MainActivity.applicationContext)
                .inflate(R.layout.item_toast, null, false)
            toast.view = view
            view.findViewById<TextView>(R.id.txtMessage).text = "سلام"
            view.findViewById<ImageView>(R.id.imgIcon)
                .setImageResource(R.drawable.ic_baseline_done_24)
            toast.setGravity(Gravity.FILL_HORIZONTAL or Gravity.BOTTOM, 0, 0)
            toast.duration = Toast.LENGTH_LONG
            toast.show()
        }
    }
}

我有两个问题。一是我必须在toast的布局中指定我的应用程序主题。如果我从布局中删除主题属性,应用程序将在任何设备上崩溃。第二个问题是,当我在安卓 api 17上运行应用程序时,它崩溃了。但当我在安卓 api 23上运行它时,它运行得很好。此外,当我使用活动上下文而不是应用程序上下文时,问题得到了解决


共 (0) 个答案