Axb*_*rov 3 java android kotlin android-resources
我正在寻找后退箭头的默认可绘制对象。在这个问题中展示了如何在 xml 中获取它。但我正在寻找在编程方面使用(java/kotlin)。
我想在如下代码中使用这个 drawable id:
ContextCompat.getDrawable(context, homeAsUpIndicatorId);
Run Code Online (Sandbox Code Playgroud)
创建可绘制对象并将可绘制对象 ID 传递给函数。例如,我创建了一个名为arrow_back.xml
.
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
android:autoMirrored="true">
<path
android:pathData="M20,11L7.8,11l5.6,-5.6L12,4l-8,8l8,8l1.4,-1.4L7.8,13L20,13L20,11z"
android:fillColor="#fff"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
注意:
homeAsUpIndicatorId
引用的可绘制对象具有私有修饰符,因此您无法直接访问它。然而,上面的代码是从 vector drawable 中复制的,几乎没有修改。
我会getDrawable()
像这样将 id 传递给函数。
ContextCompat.getDrawable(context, R.drawable.arrow_back);
Run Code Online (Sandbox Code Playgroud)
编辑:
执行以下操作以从以下位置获取可绘制对象homeAsUpIndicatorId
:
TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {R.attr.homeAsUpIndicator});
int attributeResourceId = a.getResourceId(0, 0);
ContextCompat.getDrawable(context, attributeResourceId);
a.recycle()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1109 次 |
最近记录: |