Svg 在设备中不可见,但在 android xml 中可见

2024-04-02

我对这些图标使用了 SVG,这些图标在 xml 中可见,但在 device 中不可见。 以下是我的代码:

                ` <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    android:gravity="center"
                    android:orientation="horizontal"
                    android:weightSum="10"
                   >

                    <ImageView
                        android:layout_width="0dp"
                        android:layout_height="25dp"
                        android:layout_weight="1.5"
                        app:srcCompat="@drawable/email_id_icon"
                        />

                    <EditText
                        android:layout_width="0dp"
                        android:id="@+id/et_email"
                        android:layout_height="wrap_content"
                        android:layout_weight="8"
                        android:textColor="#fff"
                        android:inputType="textEmailAddress"
                        android:background="@android:color/transparent"
                        android:hint="Email Id"
                        android:textColorHint="#fff"
                        android:imeOptions="actionNext"
                        />

                </LinearLayout>`

我已经经历过类似的问题,其中大多数建议使用android: src代替app:srcCompat,但我必须使用 srcCompat 只是因为 SVG。所以我该怎么做?

Edit -我在另一个活动的布局中做了同样的事情。 这是工作代码:

  <LinearLayout
        android:id="@+id/login_form_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:orientation="horizontal"
            android:weightSum="10">

            <ImageView
                android:id="@+id/username_iv"
                android:layout_width="0dp"
                android:layout_height="25dp"


                android:layout_weight="2"
                app:srcCompat="@drawable/email_id_icon" />

            <EditText
                android:layout_width="0dp"
                android:id="@+id/username_et"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:textColor="#fff"
                android:inputType="textEmailAddress"
                android:background="@android:color/transparent"
                android:hint="Email Id"
                android:textColorHint="#fff"
                android:imeOptions="actionNext"
                />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginBottom="10dp"
            android:background="#fff" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:orientation="horizontal"
            android:weightSum="10">

            <ImageView
                android:id="@+id/password_iv"
                android:layout_width="0dp"
                android:layout_height="25dp"
                android:layout_weight="2"
                app:srcCompat="@drawable/password_icon" />

            <EditText
                android:id="@+id/password_et"
                android:layout_height="wrap_content"
                android:layout_weight="8"
                android:layout_width="0dp"
                android:background="@android:color/transparent"
                android:textColor="#fff"
                android:hint="Password"
                android:inputType="textPassword"
                android:fontFamily="roboto"
                android:textColorHint="#fff"
                 />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginBottom="20dp"
            android:background="#fff" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn_login"
            android:layout_marginBottom="20dp"
            android:background="@drawable/login_button_style"
            android:text="Login"
            android:textSize="@dimen/headingText"
            android:textAllCaps="false"
            android:textColor="#fff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_forgetpassword"
            android:layout_marginBottom="10dp"
            android:text="Forget your password ?"
            android:textColor="#fff"
            android:textSize="@dimen/headingText" />


    </LinearLayout>

我不想有 4 种分辨率的图标,所以我制作了一个 .png 图像并使用 Android Studio 的 Vector asset 转换为 .svg 。


确保您正在使用AppCompatActivity并不是Activity。如果您正在使用Activity,然后在您的活动/片段中写入:

imageView.setImageResource(R.drawable.ic_svg_image);

还添加build.gradle:

android {  
    defaultConfig {  
        vectorDrawables.useSupportLibrary = true  
    }  
}

对于 4.x 版本的 Android,您还可以添加对TextViews' 的内部可绘制对象。这也是实际的ImageViews.

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // This flag should be set to true to enable VectorDrawable support for API < 21.
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
}

然后在AndroidManifest中添加这个文件:

<application
    android:name=".MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    ...
</application>

有时它会破坏一些旧设备。然后将您的可绘制对象包裹在里面:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_svg_image/>
</selector>

并设置它而不是 SVG:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/drawable"
    />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Svg 在设备中不可见,但在 android xml 中可见 的相关文章

  • SVG 中虚线的悬停事件

    带有线 或路径 的 SVG 使用stroke dasharray当用户将鼠标悬停在虚线的实心部分上时 似乎只会触发 CSS 和 JS 悬停事件 https codepen io anon pen YeXoZy https codepen i
  • Android:订阅 Firebase 云消息传递 (FCM) 主题

    根据Firebase 云消息传递文档 https firebase google com docs cloud messaging android send multiple 为了订阅用户的主题 我需要调用 FirebaseMessagin
  • Firebase 存储 URL 不断随着新令牌的变化而变化

    我正在尝试使用 firebase 数据库和存储构建社交媒体应用程序 以下是预期的流程 用户上传个人资料图片 该图片存储在当前用户文件夹中的 Firebase 存储中 并且 URL 存储在 Firebase 数据库中以便快速访问 工作正常 用
  • Android 相机 - 将图像保存到 SD 卡中的新文件夹中

    我有一个非常简单的应用程序 目前可以拍照然后保存图像 目前的问题是 由于某种原因 我无法找到图像在手机上的保存位置 我想要做的最终结果是 当拍摄照片时 图像会保存到 SD 卡上创建的新文件夹中 但如果该文件夹尚不存在 则必须创建该文件夹 自
  • Android Studio 2.2 更新:未使用新的 Gradle 插件 2.2.0 生成对齐的 APK (zipAlign)

    将 Android Studio 更新到版本 2 2 后 我还获得了 Gradle 插件的更新 它是 2 1 3 classpath com android tools build gradle 2 2 0 我看到未对齐的变体 APK 文件
  • 如何在 Android 中恢复我的音频?

    我必须实现用于创建具有暂停和恢复状态的音频的应用程序 当我的应用程序作为启动时音频启动 当我按下模拟器上的后退按钮时 音频音乐处于暂停状态 但是当我的活动回来时从停止状态到前台我的音频音乐未恢复 这是我的代码 public class Au
  • 如何删除带有操作栏、视图页面和多个片段的选项卡?

    我正在使用我找到的代码here https stackoverflow com questions 10082163 actionbarsherlock tabs multi fragments public class ActionBar
  • java.io.IOException:无法打开同步连接!进入 Nexus [重复]

    这个问题在这里已经有答案了 我尝试在 Eclipse 上运行我的应用程序 但发现了这些错误 这是第一次遇到 所以请给我关于这些错误的任何想法 我目前使用的是 Nexus 手机 2011 08 04 15 59 09 App Android
  • Fragment 内的 FragmentPagerAdapter

    我在实现基于多个 ViewPager 的设计时遇到了一些麻烦 在较高的层次上 我有一个 FragmentActivity 其中只有一个 FrameLayout 作为其内容 我有 3 个不同的片段想要显示 所有 3 个均为全屏 一次仅使用 1
  • 未定义的参考错误 - rand

    我正在创建一个命令行 C 测试应用程序 可执行 以便在我的 root Android 设备上运行 该可执行文件使用多个预构建的 C 库 其中之一使用 rand 在链接状态期间我收到错误 rand 的未定义引用 为了检查路径是否设置正确 我尝
  • 如何读取其他应用程序的SharedPreferences(相同用户ID)?

    在安卓4 3上测试 我有两个应用程序 com my app first and com my app second 在我的活动中 我想读取其他应用程序的首选项 我选择对我的两个应用程序使用相同的用户 ID android sharedUse
  • PhoneGap BarcodeScanner - ClassNotFound

    UPDATE 2 我发布了一个解决我原来问题的答案 看欲了解更多信息 https stackoverflow com a 9541490 398519 UPDATE供任何想知道的人参考 最后我发现了这个 http github com co
  • Android - 框架布局高度与协调器布局不匹配

    我的 FrameLayout 抽屉布局中的容器 有问题 FrameLayout的高度超过了屏幕高度 在底部的android默认菜单按钮下方
  • Android facebook api,获取不同尺寸的头像

    我正在使用相对较新的 Facebook 图形 api 我正在获取使用该应用程序的朋友列表及其个人资料照片 我不知道如何修改我发送的参数 以便返回的图片很大 当前返回的默认值很小 我正在使用 newMyFriendsRequest 我发送的参
  • Android 以编程方式停止 toast 通知?

    有没有办法以编程方式停止 Toast 消息 假设我有一个按钮 单击它可以滚动 toast 消息 并且在 onclick 事件中我想停止队列中的所有消息并只显示新消息 我该怎么做 我的代码的简化版本如下 代码 public class Hel
  • Android Studio gradle 构建时间太长

    My 安卓工作室项目过去构建速度更快 但现在需要很长时间才能构建 有什么想法可能导致延误吗 我努力了https stackoverflow com a 27171878 391401 https stackoverflow com a 27
  • Android:非键盘输入法

    我正在尝试为 Android 创建一个不是传统键盘 对应于不同字母的按键行 的 IME 并且我无法找到有关如何执行此操作的有用资源 因为 SDK 中的所有代码示例都使用键盘 API它是内置函数 我在 XML 文件中设计了 IME 界面 就好
  • 根据 ss:Index 对单元格进行排序

    服务器生成的 XML 电子表格具有随机序列
  • 即使提供了通知,也无法在前台服务中运行蓝牙扫描超过 10 - 15 秒

    即使应用程序关闭 我也使用服务来保留对 Android 手机的扫描 我使用广播接收器在被杀死时重新启动我的服务 它会重新启动扫描 并且只工作大约 15 秒 然后停止 当我单击 MainActivity 中的 button1 时 我启动了服务
  • 如何获取 res.drawable 文件夹的路径来复制文件?

    我正在编写我的应用程序AndroidStudio 我的里面有gif文件drawable gifs文件夹 我希望将该文件复制到MediaStore Images Media单击按钮后的文件夹 目前 即使使用发布的一些答案 我也无法获取我的 g

随机推荐

  • 监视一组文件的更改并在更改时对其执行命令

    我想到的 命令行 界面如下所示 watching FILE do COMMAND ARGS and COMMAND ARGS 凡出现 in COMMAND替换为已更改的文件的名称 并注意 do and and 是关键字 例如 gt watc
  • Ajax 请求 net_error = -3 (ERR_ABORTED)

    我似乎无法弄清楚为什么会发生这种情况 它只是偶尔发生一次 我应该指出 如果这有什么影响的话 我正在使用卫星互联网 在网络间隔中 这是我捕获的 2725 URL REQUEST https testsite com wp json api a
  • OS X / iOS - 使用 AudioConverterFillComplexBuffer 进行缓冲区的采样率转换

    我正在为一个项目编写一个 CoreAudio 后端名为 XAL 的音频库 http libxal googlecode com 输入缓冲器可以具有不同的采样率 我使用单个音频单元进行输出 想法是在将缓冲区发送到音频单元之前对其进行转换和混合
  • Android 无法使用 soundpool 播放某些 wav 文件?

    某些 wav 文件我无法使用 soundpool 播放 我什么也听不到 有些文件播放得很好 为什么 code AudioManager mgr AudioManager context getSystemService Context AU
  • 如何将矩阵(列表列表)中的所有值增加 n?

    我必须创建一个函数 将矩阵作为参数传递 然后将矩阵中每个项目的值增加n通过使用嵌套循环 例如 如果我的矩阵是 8 9 4 6 7 2 and n 1 我想要的输出是 9 10 5 7 8 3 您可以编写一个简单的函数来迭代列表 以将每个元素
  • cygwin rsync协议错误

    我正在尝试在 64 位 Windows 7 和 64 位 Linux 之间 rsync 文件 我在两台机器上都安装了 rsync 3 0 7 Windows 上的 cygwin 版本 这是我在 Windows 上运行的命令 我看到它正在建立
  • 初始数据夹具中的用户

    我在默认情况下创建了一些用户fixtures initial data json从而有一些测试 科目 我遇到的问题是密码生成 我可以在 字段 中设置密码 但这不会生成哈希密码 model auth user pk 1 fields user
  • std::make_unique 和 std::unique_ptr 与 new 之间的差异

    Does std make unique有任何效率优势 例如std make shared 与手动构建相比std unique ptr std make unique
  • 有序列表 CSS 样式包括父编号

    我们希望使用 CSS 创建一个如下所示的有序列表 A A 1 A 2 B C C 1 C 2 C 2 1 C 2 2 您如何将父索引包含在子索引中 你需要使用CSS 计数器 https developer mozilla org en US
  • Chart.js 将标签变成链接

    我不确定如果不做以下事情是否可能 在 HTML 画布中创建链接 https stackoverflow com questions 6215841 create links in html canvas但让我们确定一下 有没有一种方法 相对
  • 计算 python 中第二个列表中列表项的出现次数

    a list 1 2 3 4 5 6 7 8 9 0 b list 1 3 6 9 如何计算列表be中的某个项目在列表a中出现的次数 上面的示例应返回值 4 在写这个问题时 我想到了以下内容 似乎有效 a list 1 2 3 4 5 6
  • C++ 容器的一般用例

    的一般用例是什么C 标准库容器 http www cplusplus com reference stl bitset deque list map multimap multiset 优先队列 queue set stack vector
  • Linq 在 C# 中比较两个集合

    我想比较我当前正在使用嵌套 for 循环执行的 C 中的两个集合 Linq 中是否有一种方法可以更快 更高效地执行相同操作 这是我当前的代码 它可以完美地工作 只是在寻找一种有效的方法 OrgCollection myYears Org R
  • 足球(足球)场线的单应性

    我正在研究使用足球比赛的视频 并尝试使用单应性将帧映射到球场的俯视图 我已经开始使用霍夫线以及线段检测器找到帧中的所有白线 其中线段检测器似乎工作得稍微好一些 请参阅下面我的代码和示例 import cv2 import numpy as
  • Scala 中的 shouldBe 和 shouldEqual 有什么区别?

    我什么时候应该使用shouldBe 什么时候应该使用shouldEqual port shouldEqual 8000 port shouldBe 8000 From http www scalatest org user guide us
  • 设置 ng-htmljs-preprocessor karma 预处理器

    我正在设置 Karma 配置文件 但我不完全理解存在的一些选项 因为我没有成功测试已通过 ngHtml2JsPreprocessor 运行并已 templateCached 在 ngHtml2JsPreprocessor 内部 我可以添加一
  • 无法从 Git post-receive hook 检测分支

    我在远程存储库上设置了一个后接收挂钩 它尝试确定传入推送的分支名称 如下所示 branch git rev parse abbrev ref HEAD 不过 我发现无论我从 branch 变量推送哪个分支 都会设置为 master 有任何想
  • 如何在 Visual Studio 2017 中将 npm 包与 ASP.NET CORE 2 一起使用?

    我在 Visual Studio 2017 中向我的 ASP NET Core 2 项目添加了一些 npm 包 现在我想使用这些包中的 css 和 js 文件 但 VS 看不到它们 因为 node modules 文件夹位于 wwwroot
  • 正则表达式:重复组仅获取最后一组

    My data stack 123 overflow 456 others st 7 ov 7 againothers m 11 t 12 m 13 t 14 m 15 t 16 st 8 ov 8 againothers m 17 t 1
  • Svg 在设备中不可见,但在 android xml 中可见

    我对这些图标使用了 SVG 这些图标在 xml 中可见 但在 device 中不可见 以下是我的代码