[Android] view. getRootView () real meaning and test, getrootview

Source: Internet
Author: User

[Android] view. getRootView () real meaning and test, getrootview

View. getRootView (): Finds the topmost view in the current view hierarchy.

My understanding is to find the root view of the view level of the view instance.


To verify the true meaning of this view. getRootView (), I tested it below:

Activity_main.xml:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">  <include       layout="@layout/test_layout"/></AbsoluteLayout>

Test_layout.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <RelativeLayout         android:layout_width="wrap_content"        android:layout_height="wrap_content">        <Button                android:id="@+id/testBtn"   android:layout_width="match_parent"   android:layout_height="wrap_content"/>    </RelativeLayout></LinearLayout>

MainActivity. java:

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button testBtn = (Button) findViewById(R.id.testBtn);Log.i("testBtn", testBtn.toString()+"  id:"+testBtn.getId());Log.i("testBtn's RootView",testBtn.getRootView().toString()+"  id:"+testBtn.getRootView().getId());View testView =LayoutInflater.from(this).inflate(R.layout.test_layout, null);Button testBtn2 = (Button) testView.findViewById(R.id.testBtn);Log.i("testBtn2", testBtn2.toString()+"  id:"+testBtn2.getId());Log.i("testBtn2's RootView",testBtn2.getRootView().toString()+"  id:"+testBtn2.getRootView().getId());View decorView = getWindow().getDecorView();View contentView =decorView.findViewById(android.R.id.content);View mainRootView =((ViewGroup) contentView).getChildAt(0);Log.i("decorView", decorView.toString()+"  id:"+decorView.getId());Log.i("contentView", contentView.toString()+"  id:"+contentView.getId());Log.i("mainRootView",mainRootView.toString()+"  id:"+mainRootView.getId());}}

Print result:



From the print results, we need to note that although testBtn and testBtn2 have the same id, they are different instances and their view levels are different, therefore, the root view obtained through getRootView is different.


Finally, we can see that you can use

View rootView = ((ViewGroup) (getWindow().getDecorView().findViewById(android.R.id.content))).getChildAt(0);

.


What does android mean?

Let me tell you that the current smart phones are mainly equipped with four operating systems: Saipan, WM, iphono, and android. Android is the most popular among them.

Meaning of test name

Go to the package network.
Www.mcbao.com/
Find the one you want and name it.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.