Android ImageView 图片未显示

Android ImageView image not showing

我目前正在使用 imageview 来显示矢量可绘制对象。它不会显示,但它会在左侧的 androidstudio 中显示图标。代码如下:

编辑 我将权重设置为 0 以将其显示在线性布局的末尾。它不会干扰宽度或高度。 布局不是问题,显示是

如您所见,向量在左侧有效

弹窗代码:

<?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:id="@+id/report_template_edit_draft_popup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@color/pspdf__color_white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="@string/report_template_popup_window_title"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/report_template_popup_close_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0"
                android:minHeight="30dp"
                android:minWidth="30dp"
                app:srcCompat="@drawable/ic_close" />


    </LinearLayout>

</LinearLayout>

问题是它没有出现在设计中而不是应用程序中。

是的,我在 build.gradle

中使用它
vectorDrawables.useSupportLibrary true

ic_close.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportHeight="24.0"
        android:viewportWidth="24.0">
    <path
        android:fillColor="#ffffff"
        android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

app:srcCompat 必须与 AppCompatImageView 一起使用。如果要使用ImageView,则必须使用android:src

改成这样:

<ImageView 
    <!-- your other attributes -->
    android:src="@drawable/ic_close"/>

或者这样:

<android.support.v7.widget.AppCompatImageView
                        <!-- your other attributes -->
                        app:srcCompat="@drawable/ic_close"

                />

编辑:

也删除 android:layout_weight="0" 表格 ImageView 并用 TextView

中的 android:layout_width="0dp" 替换 android:layout_width="match_parent"

编辑 2:

<android.support.v7.widget.AppCompatImageView
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:scaleType="fitCenter"
                        app:srcCompat="@drawable/ic_close"
                        android:adjustViewBounds="true"

                />

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".

https://developer.android.com/guide/topics/ui/layout/linear

试试这个

<android.support.v7.widget.AppCompatImageView
android:id="@+id/reprt_template_popup_close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
androi:minHeight="30dp"
androi:minWidth="30dp"
app:srcCompat="@drawable/ic_close"
/>

上面那个不需要权重

让它像你在图片中显示的那样

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="@string/report_template_popup_window_title"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/report_template_popup_close_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:minHeight="30dp"
            android:minWidth="30dp"
            app:srcCompat="@drawable/ic_close" />
</LinearLayout>

看看下面的布局,我使用 android:weightSum 为视图正确分配权重。

<?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:id="@+id/report_template_edit_draft_popup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.1"
                android:gravity="center_vertical"
                android:maxLines="1"
                android:text=" i am ravibndra kushwaha have alook on the solution"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/report_template_popup_close_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0.9"
                android:minHeight="30dp"
                android:minWidth="30dp"
                app:srcCompat="@drawable/ic_close" />


        </LinearLayout>

    </LinearLayout>
</LinearLayout>

首先不用加android:layout_weight="0",可以像..

<?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:id="@+id/report_template_edit_draft_popup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

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

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="some text"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/report_template_popup_close_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:minHeight="30dp"
                android:minWidth="30dp"
                app:srcCompat="@drawable/ic_dummy" />


        </LinearLayout>

    </LinearLayout>`
</LinearLayout>

其次,尝试使用不同的矢量图标或更改布局背景颜色或矢量图标颜色,可能是白色超过白色的问题。