开发者

Android: Making changes to a button's parent view

开发者 https://www.devze.com 2023-02-04 02:04 出处:网络
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 par

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 (开发者_C百科I have very good reasons for not wanting this). Isn't there a way to just access the parent view from the button itself?


Try View.getParent():

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

精彩评论

暂无评论...
验证码 换一张
取 消