简体   繁体   English

从GetBackground获取资源ID

[英]Get resource id from GetBackground

I created a component having inherited it from View. 我创建了一个从View继承下来的组件。 Tell me please, how I can get resource id (int) from drawable (GetBackground function). 请告诉我,我如何从drawable(GetBackground函数)获取资源ID(int)。 Thanks. 谢谢。

It's easy! 这很容易!

Step 1: 第1步:

When background resource is being set, save the id in an int. 设置后台资源后,将ID保存为int。

int backgroundId;
onButton1Click(View v) {
backgroundId = R.drawable.myBackgroundResource;
((yourViewType)findViewById(R.id.myView)).setBackgroundResource(backgroundId);
}

Step 2: 第2步:

Use your saved int. 使用您保存的int。

onButton2Click(View v) {
// Your background id is known here.
someView.setBackgroundResource(backgroundId);
// Or some other uses!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM