Android ImageView图片未显示

时间:2018-09-18 06:32:51

标签: android image vector imageview

我目前正在使用imageview显示可绘制的矢量。它不会显示,但会在左侧的androidstudio中显示图标。该代码可以在下面看到:

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

如您所见,向量在左侧有效 enter image description here

弹出代码:

<?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>

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

enter image description here

是的,我正在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>

4 个答案:

答案 0 :(得分:1)

首先,无需添加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>

第二,尝试使用其他矢量图标或更改布局背景颜色或矢量图标颜色,这可能是白色白色的问题。

答案 1 :(得分:0)

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"的{​​{1}}表格 并将ImageView替换为TextView中的android:layout_width="match_parent"

EDIT2:

android:layout_width="0dp"
  

创建一个线性布局,其中每个孩子使用相同数量的   在屏幕上的空白处,将每个视图的android:layout_height设置为   “ 0dp”(用于垂直布局)或每个视图的android:layout_width   到“ 0dp”(用于水平布局)。然后设置android:layout_weight   每个视图的“ 1”。

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

答案 2 :(得分:0)

尝试一下

<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>

答案 3 :(得分:0)

看看下面的布局,我已经使用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>
相关问题