相关疑难解决方法(0)

在前Lollipop崩溃时使用Android矢量Drawables

我在Lollipop之前使用android中的矢量drawables,这些是我的一些库和工具版本:

  • Android Studio:2.0
  • Android Gradle插件:2.0.0
  • 构建工具:23.0.2
  • Android支持库:23.3.0

我在我的应用级别添加了此属性 Build.Gradle

android {  
  defaultConfig {  
    vectorDrawables.useSupportLibrary = true  
   }  
}
Run Code Online (Sandbox Code Playgroud)

还值得一提的是,我使用了一个额外的drawable,如Android官方博客(此处链接)中所述的LayerDrawable(layer_list),用于设置外部矢量绘图的drawablesapp:srcCompat

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/search"/>
</level-list>
Run Code Online (Sandbox Code Playgroud)

你会发现在app之外直接引用矢量drawable:srcCompat将在Lollipop之前失败.但是,AppCompat确实支持在另一个可绘制容器(如StateListDrawable,InsetDrawable,LayerDrawable,LevelListDrawable和RotateDrawable)中引用它们时加载矢量drawable.通过使用此 间接,您可以在TextView的android:drawableLeft属性中使用向量drawable,这通常不能支持向量drawable.

当我使用的app:srcCompat一切工作正常,但当我使用时:

android:background
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom
Run Code Online (Sandbox Code Playgroud)

ImageView,ImageButton,TextViewEditText到棒棒糖之前,它引发厚望:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/search_toggle.xml from drawable resource ID #0x7f0200a9
Run Code Online (Sandbox Code Playgroud)

android vector android-compatibility android-support-library android-drawable

81
推荐指数
9
解决办法
5万
查看次数

在Android上使用SVG图标的最佳做法是什么?

我即将创建我的第一个Android 原生(因此不是基于浏览器的)应用程序,并寻找有关图标创建/配置的一些好的做法.由于它应该支持多个设备/分辨率,我认为最好使用SVG来创建它们.至少有这个lib:http://code.google.com/p/svg-android/承诺在Android上提供对SVG的支持.

到目前为止,我还没有找到描述这个或另一个库的使用的资源作为在设备上呈现SVG图标的方法,所以我有点不愿意使用它.到目前为止,我所看到的最好的是使用SVG作为在不同分辨率下预渲染基于png的图标的源格式.

所以我的问题是:SVG图标是一个很好的选择,可以直接在设备上使用而无需png预渲染步骤(它是否可以工作),如果,为什么似乎没有人使用这种方法?

icons svg android

72
推荐指数
7
解决办法
8万
查看次数

Android:底部导航视图 - 更改所选项目的图标

BottomNavigationView在我的应用程序中添加了像.

main.xml中

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white"
        app:menu="@menu/bottom_navigation_main" />
Run Code Online (Sandbox Code Playgroud)

bottom_navigation_main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_favorites"
        android:enabled="true"
        android:icon="@drawable/ic_favorite_white_24dp"
        android:title="@string/text_favorites"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_schedules"
        android:enabled="true"
        android:icon="@drawable/ic_access_time_white_24dp"
        android:title="@string/text_schedules"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_music"
        android:enabled="true"
        android:icon="@drawable/ic_audiotrack_white_24dp"
        android:title="@string/text_music"
        app:showAsAction="ifRoom" />
</menu>
Run Code Online (Sandbox Code Playgroud)

MainActivity点击

bottomNavigationView.setOnNavigationItemSelectedListener(
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_favorites:
                        //need change icon of favotites here.
                    case R.id.action_schedules:

                    case R.id.action_music:

                }
                return true;
            }
        });
Run Code Online (Sandbox Code Playgroud)

我想更改所选位置底部导航的图标.当用户点击一个项目时,我们如何实现此功能?

(如果用户点击了一个项目,那么图标会变为另一个项目)

android android-layout android-support-library bottomnavigationview

41
推荐指数
6
解决办法
6万
查看次数

ResourcesCompat.getDrawable()vs AppCompatResources.getDrawable()

我对这两个API有点困惑.

ResourcesCompat.getDrawable(Resources res,int id,Resources.Theme theme)

返回与特定资源ID关联的可绘制对象,并为指定的主题设置样式.将根据底层资源返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等.

在API级别21之前,不会应用主题,此方法只需调用getDrawable(int).

AppCompatResources.getDrawable(Context context,int resId)

返回与特定资源ID关联的可绘制对象.

此方法支持在没有平台支持的设备上对矢量动画矢量资源进行充气.

  1. 这两个类之间有什么显着差异(除了矢量膨胀)?
  2. 我应该选择哪一个?为什么?

java android android-theme android-resources android-styles

26
推荐指数
3
解决办法
8226
查看次数

23.2.0在4.X中将矢量绘制为背景

我对使用app:srcCompat="@drawable/icon"support-library 23.2.0中引入的属性将矢量drawables设置为布局的新可能性感到非常兴奋.

但我想知道如何以编程方式获取其中一个drawables或者设置为背景.

我想到了类似的东西: ContextCompat.getDrawable(context, R.drawable.icon)

这甚至可能吗?

android android-support-library

16
推荐指数
1
解决办法
9424
查看次数

如何正确使用向后兼容的Vector Drawable与最新的Android支持库?

不久之前,矢量drawable已添加到支持库中,从那时起API中发生了很多变化:Gradle标志,初始化块,选择器,自定义XML属性等.问题是 - 如何正确使用它(在这些情况下支持lib v25):

  • ImageView的
  • TextView drawable
  • 菜单图标
  • 通知图标

XML和编程.

java android android-appcompat android-vectordrawable

6
推荐指数
1
解决办法
3592
查看次数

Android - 具有不同颜色和角度的背景颜色

我试图弄清楚如何在xml中创建这个背景,找不到任何帮助我的东西.

背景

忽略孩子和文本的圆圈图像,当然,我在谈论背景本身,有没有办法在xml中创建类似的东西?

在此先感谢您的帮助.干杯!

android android-xml android-view android-shape

5
推荐指数
0
解决办法
141
查看次数

Android - 支持Motorola上的矢量绘图

我在摩托罗拉预棒棒糖手机上渲染矢量绘图时遇到问题.我在Moto G和其他KitKat上进行了测试.每次我开始应用程序时,一些图标看起来已损坏,有些图标完全缺失.每次发布后,它们都会以不同的方式被破坏.在联想,三星,AOSP模拟器和其他JB +到Nougat的一切都可以.只有摩托罗拉手机才能很好地使用支持库渲染矢量绘图.有没有人有同样的问题?

android motorola support-vector-compat

5
推荐指数
1
解决办法
169
查看次数

DrawableLeft with xml icon

How to use drawableLeft with an xml icon? I have the following button:

<Button
   android:id="@+id/vitimas"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginBottom="10dp"
   android:layout_marginLeft="20dp"
   android:layout_marginRight="20dp"
   android:drawableLeft="@drawable/ic_person_black_24dp"
   android:drawableStart="@drawable/ic_person_black_24dp"
   android:background="@drawable/botao_verde"
   android:text="Vítimas"/>
Run Code Online (Sandbox Code Playgroud)

In old APIs such as 16, the app stops working due to drawableLeft, I tried to use an ImageButton but the same happens, if I use app: srcCompat it works, however the icon is not stay in left, I need it to be stay in left and the text in the middle

The icon is …

android android-layout

2
推荐指数
1
解决办法
2615
查看次数