I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick
event where the argument is a View. But how can I get the view in an activity?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
to get View of the current Activity
in any onClick we will be getting "View view", by using 'view' get the rootView.
View view = view.getRootView();
and to get View in fragment
View view = FragmentClass.getView();
Just incase Someone needs an easier way:
The following code gives a view of the whole activity:
View v1 = getWindow().getDecorView().getRootView();
To get a certian view in the activity,for example an imageView inside the activity, simply add the id of that view you want to get:
Hope this helps somebody
anyview.getRootView();
will be the easiest way.If you need root view of your activity (so you can add your contents there) use
Also it was reported that on some devices you have to use
instead.
Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most devices it is not).
If you need to get view that you added to your activity using
setContentView()
method then as pottedmeat wrote you can useBut better just set id to this view in your xml layout and use this id instead.
This is what I use to get the root view as found in the XML file assigned with
setContentView
:I tested this in android 4.0.3, only:
give the same view what we get from
and
giving child of its
Please confirm.