Imageview在线性布局中不可见

时间:2019-09-26 05:48:17

标签: android xml imageview android-vectordrawable

我正在尝试在线性布局中放置一个imageview,但是由于某种原因它似乎没有显示。

这是我的布局xml文件-


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

    <ImageView
        android:id="@+id/frag_discover_search_image_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        android:src="@drawable/ic_icon_search_explore" />


    <com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayout
        android:id="@+id/frag_discover_swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srl_direction="bottom">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/frag_discover_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            tools:listitem="@layout/row_video_insta" />
    </com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayout>

    <include layout="@layout/progress_bar" />
</LinearLayout>


这是预览时的布局-

enter image description here

如您所见,在视图组的左上方有一个放置图像的位置,但是我找不到图像不可见和不可点击的原因。有任何想法吗?

这是“ ic_icon_search_explore”文件-


<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="16dp"
    android:height="16dp"
    android:viewportWidth="16"
    android:viewportHeight="16">
  <path
      android:pathData="M-4,-4h24v24h-24z"
      android:strokeAlpha="0"
      android:strokeWidth="1"
      android:fillColor="#D8D8D8"
      android:fillType="nonZero"
      android:strokeColor="#00000000"
      android:fillAlpha="0"/>
  <path
      android:pathData="M6.8333,6.8333m-5.8333,0a5.8333,5.8333 0,1 1,11.6667 0a5.8333,5.8333 0,1 1,-11.6667 0"
      android:strokeLineJoin="round"
      android:strokeWidth="1.5"
      android:fillColor="#00000000"
      android:strokeColor="#FFFFFF"
      android:fillType="evenOdd"
      android:strokeLineCap="round"/>
  <path
      android:pathData="M15,15L10.9556,10.9556"
      android:strokeLineJoin="round"
      android:strokeWidth="1.5"
      android:fillColor="#00000000"
      android:strokeColor="#FFFFFF"
      android:fillType="evenOdd"
      android:strokeLineCap="round"/>
</vector>


3 个答案:

答案 0 :(得分:1)

  

使用此可绘制对象。您只需要更改图标的颜色即可。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="16dp"
        android:height="16dp"
        android:viewportWidth="16"
        android:viewportHeight="16">
    <path
            android:pathData="M-4,-4h24v24h-24z"
            android:strokeAlpha="0"
            android:strokeWidth="1"
            android:fillColor="#000"
            android:fillType="nonZero"
            android:strokeColor="#00000000"
            android:fillAlpha="0"/>
    <path
            android:pathData="M6.8333,6.8333m-5.8333,0a5.8333,5.8333 0,1 1,11.6667 0a5.8333,5.8333 0,1 1,-11.6667 0"
            android:strokeLineJoin="round"
            android:strokeWidth="1.5"
            android:fillColor="#00000000"
            android:strokeColor="#000"
            android:fillType="evenOdd"
            android:strokeLineCap="round"/>
    <path
            android:pathData="M15,15L10.9556,10.9556"
            android:strokeLineJoin="round"
            android:strokeWidth="1.5"
            android:fillColor="#00000000"
            android:strokeColor="#000"
            android:fillType="evenOdd"
            android:strokeLineCap="round"/>
</vector>

您的图像清晰可见,但图像的颜色为白色,因此未在布局中显示。

答案 1 :(得分:1)

您的布局没有问题

问题出在您的ic_icon_search_explore图标颜色上

图标和布局的颜色为白色,这就是为什么图标未显示在布局中的原因

只需更改您的ic_icon_search_explore的图标颜色即可

 <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="16dp"
        android:height="16dp"
        android:viewportWidth="16"
        android:viewportHeight="16">
    <path
            android:pathData="M-4,-4h24v24h-24z"
            android:strokeAlpha="0"
            android:strokeWidth="1"
            android:fillColor="#D8D8D8"
            android:fillType="nonZero"
            android:strokeColor="#00000000"
            android:fillAlpha="0"/>
    <path
            android:pathData="M6.8333,6.8333m-5.8333,0a5.8333,5.8333 0,1 1,11.6667 0a5.8333,5.8333 0,1 1,-11.6667 0"
            android:strokeLineJoin="round"
            android:strokeWidth="1.5"
            android:fillColor="#00000000"
            android:strokeColor="#ff00"
            android:fillType="evenOdd"
            android:strokeLineCap="round"/>
    <path
            android:pathData="M15,15L10.9556,10.9556"
            android:strokeLineJoin="round"
            android:strokeWidth="1.5"
            android:fillColor="#00000000"
            android:strokeColor="#ff00"
            android:fillType="evenOdd"
            android:strokeLineCap="round"/>
</vector>

答案 2 :(得分:1)

不可见,因为您在ImageView中设置了填充,因此填充会按尺寸压缩图像。设置矢量图像高度宽度时,将ImageView高度宽度设置为 wrap_content

删除填充:-

android:paddingStart="16dp"
android:paddingEnd="16dp"

尝试使用此ImageView:-

<ImageView
     android:id="@+id/frag_discover_search_image_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_icon_search_explore" />
相关问题