我在Lollipop之前使用android中的矢量drawables,这些是我的一些库和工具版本:
我在我的应用级别添加了此属性 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
,TextView
或EditText
到棒棒糖之前,它引发厚望:
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
我即将创建我的第一个Android 原生(因此不是基于浏览器的)应用程序,并寻找有关图标创建/配置的一些好的做法.由于它应该支持多个设备/分辨率,我认为最好使用SVG来创建它们.至少有这个lib:http://code.google.com/p/svg-android/承诺在Android上提供对SVG的支持.
到目前为止,我还没有找到描述这个或另一个库的使用的资源作为在设备上呈现SVG图标的方法,所以我有点不愿意使用它.到目前为止,我所看到的最好的是使用SVG作为在不同分辨率下预渲染基于png的图标的源格式.
所以我的问题是:SVG图标是一个很好的选择,可以直接在设备上使用而无需png预渲染步骤(它是否可以工作),如果,为什么似乎没有人使用这种方法?
我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
我对这两个API有点困惑.
ResourcesCompat.getDrawable(Resources res,int id,Resources.Theme theme)
返回与特定资源ID关联的可绘制对象,并为指定的主题设置样式.将根据底层资源返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等.
在API级别21之前,不会应用主题,此方法只需调用getDrawable(int).
AppCompatResources.getDrawable(Context context,int resId)
返回与特定资源ID关联的可绘制对象.
此方法支持在没有平台支持的设备上对矢量和动画矢量资源进行充气.
我对使用app:srcCompat="@drawable/icon"
support-library 23.2.0中引入的属性将矢量drawables设置为布局的新可能性感到非常兴奋.
但我想知道如何以编程方式获取其中一个drawables或者设置为背景.
我想到了类似的东西:
ContextCompat.getDrawable(context, R.drawable.icon)
这甚至可能吗?
不久之前,矢量drawable已添加到支持库中,从那时起API中发生了很多变化:Gradle标志,初始化块,选择器,自定义XML属性等.问题是 - 如何正确使用它(在这些情况下支持lib v25):
XML和编程.
我在摩托罗拉预棒棒糖手机上渲染矢量绘图时遇到问题.我在Moto G和其他KitKat上进行了测试.每次我开始应用程序时,一些图标看起来已损坏,有些图标完全缺失.每次发布后,它们都会以不同的方式被破坏.在联想,三星,AOSP模拟器和其他JB +到Nougat的一切都可以.只有摩托罗拉手机才能很好地使用支持库渲染矢量绘图.有没有人有同样的问题?
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 …