我希望获得对图层列表中的项目的引用,以便以编程方式更改某些属性,但我看不出任何差异,看起来它不起作用。
这是我的代码:
logo.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/circle">
<shape android:shape="oval">
<solid android:color="#fff"/>
<size android:height="33px" android:width="33px"/>
<stroke android:color="#000" android:width="2px"/>
</shape>
</item>
</layer-list>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shahin.testing2.MainActivity">
<Button
android:layout_width="33px"
android:id="@+id/button"
android:layout_height="33px"
android:background="@drawable/logo"/>
</LinearLayout>
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayerDrawable layerDrawable = (LayerDrawable)
ContextCompat.getDrawable(getApplicationContext(),R.drawable.logo);
GradientDrawable gradientDrawable = (GradientDrawable)
layerDrawable.findDrawableByLayerId(R.id.circle);
gradientDrawable.setStroke(10, Color.BLUE);
Button button = (Button)findViewById(R.id.button);
button.setBackgroundResource(R.drawable.logo);
}
在这种情况下,当我改变笔画的颜色和宽度时,没有出现差异,有什么问题?