SVG在设备中不可见但在android xml中可见 [英] Svg not visible in device but visible in android xml

查看:82
本文介绍了SVG在设备中不可见但在android xml中可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将SVG用于这些图标,这些图标在xml中可见,但在device中不可见.以下是我的代码:

I have used SVG's for these icons, the icons are visible in xml but not visible in device . following is my code :

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

我在SO上也遇到了类似的问题,其中大多数建议使用 android:src 而不是 app:srcCompat ,但是由于SVG,我不得不使用srcCompat.所以我该怎么做?

I have gone through similar questions on SO, most of them suggested to use android: src instead of app:srcCompat , but I have to use srcCompat only because of SVGs. So what should I do?

编辑-我在另一项活动的布局中做了同样的事情.这是工作代码:

Edit - I have done the same thing in another activity's layout . here is the working code :

  <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资源将其转换为.svg.

I did not want to have icons in 4 resolutions so I made a single .png image and converted into .svg using Android Studio's Vector asset .

推荐答案

请确保您使用的是 AppCompatActivity ,而不是 Activity .如果您使用的是 Activity ,请输入您的活动/片段:

Make sure you are using AppCompatActivity and not Activity. If you are using Activity, then write in your activity / fragment:

imageView.setImageResource(R.drawable.ic_svg_image);

还添加 build.gradle :

android {  
    defaultConfig {  
        vectorDrawables.useSupportLibrary = true  
    }  
}

对于Android的4.x版本,您还可以添加对 TextView 的内部可绘制对象的支持.对于 ImageView s也是这样.

And for 4.x versions of Android you can also add a support for TextViews' inner drawables. This is also actual for 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中添加此文件:

Then in AndroidManifest add this file:

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

有时它会在某些旧设备上崩溃.然后将您的可绘制对象包裹在里面:

Sometimes it crushes on some old devices. Then wrap your drawable inside:

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

and set it instead of SVG:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/drawable"
    />

这篇关于SVG在设备中不可见但在android xml中可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆