简体   繁体   English

Android 通知自定义布局 XML imageview 未显示

[英]Android notification custom layout XML imageview not showing

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:gravity="center"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:paddingStart="24dp"
        android:paddingBottom="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/pushIcon"
                android:layout_width="16dp"
                android:layout_height="16dp"
                app:srcCompat="@drawable/push_icon" />

            <TextView
                android:id="@+id/textView23"
                style="@style/TextAppearance.Compat.Notification.Info.Media"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="6dp"
                android:text="YOLOL"
                android:textColor="@color/browser_actions_title_color" />
        </LinearLayout>

        <TextView
            android:id="@+id/text_view_collapsed_1"
            style="@style/TextAppearance.Compat.Notification.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="New Message!"
            android:textColor="@color/colorPrimary" />

        <TextView
            android:id="@+id/text_view_collapsed_2"
            style="@style/TextAppearance.Compat.Notification.Info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Expand to show!" />
    </LinearLayout>

    <ImageView
        android:id="@+id/notifPreviewImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:srcCompat="@drawable/bb" />

</LinearLayout>

The pushIcon ImageView takes the place but is invisible, any idea why? pushIcon ImageView 代替但不可见,知道为什么吗?

A quick tip that always helps in the debug process - add a background color to the view in question (also helps to set a specific Width and Height as well for testing) - it will enable you to see if the view is being rendered at all or not.在调试过程中始终有帮助的快速提示 - 为相关视图添加背景颜色(也有助于设置特定的宽度和高度以进行测试) - 它将使您能够查看视图是否正在渲染或不。 If you see the background color, then the view is being rendered properly, but the image is not loading correctly.如果您看到背景颜色,则视图正在正确渲染,但图像未正确加载。 Without knowing this, it could be any combination of those issues.在不知道这一点的情况下,它可能是这些问题的任意组合。

With that aside, if you are dynamically changing the icon at runtime, I would look at your code there.除此之外,如果您在运行时动态更改图标,我会在那里查看您的代码。 If this is a static image then I would check that your resource file is a Vector drawable as that is only allowed when using app:srcCompat="@drawable/push_icon"如果这是 static 图像,那么我会检查您的资源文件是否是可绘制的矢量,因为只有在使用app:srcCompat="@drawable/push_icon"时才允许这样做

If your file is a non-vector drawable, then use android:src="@drawable/push_icon"如果您的文件是非矢量可绘制文件,则使用android:src="@drawable/push_icon"

Here's a discussion that elaborates on this in more detail. 这是一个更详细地阐述了这一点的讨论。

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

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