Android: Making changes to a button's parent view

半世苍凉 提交于 2019-12-05 09:34:22

问题


I have a RelativeLayout with a button inside it. Once the user clicks on that button, I would like to change the background of the parent view (RelativeLayout). I know I can do this by storing the parent view in a variable or setting a tag on the button, but I'd lime to avoid that (I have very good reasons for not wanting this). Isn't there a way to just access the parent view from the button itself?


回答1:


Try View.getParent():

Button yourBtn = (Button) findViewById(R.id.your_btn);
RelativeLayout yourRelLay = (RelativeLayout) yourBtn.getParent();


来源:https://stackoverflow.com/questions/4655845/android-making-changes-to-a-buttons-parent-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!