简体   繁体   中英

Android: Can't find outer most view inside included layout

Current behavior

The contents of the include tag are being displayed, however, when I try finding the outer view within the included layout, it returns null.

Question What am I doing wrong?

Code

Here's the java code inside onCreate() :

input_box_wrapper = findViewById( R.id.input_box_layout ).findViewById( R.id.input_box_wrapper );

if ( input_box_wrapper == null )
    Log.i( "another", "it's null" ); //this prints out

Here's the include tag inside some activity's xml file:

<include layout="@layout/msg_input_box" 
         android:id="@+id/input_box_layout"/>

Here's the included xml code inside of msg_input_box.xml :

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/input_box_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
android:background="@drawable/transparent_background2" >

    <TextView 
    android:id="@+id/send_msg_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@drawable/transparent_background2"
    android:text="Send" />

    <EditText
    android:id="@+id/input_box"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@id/send_msg_button"
    android:layout_toLeftOf="@id/send_msg_button"
    android:background="#00000000"
    android:hint="@string/type_a_message"
    android:textColorHint="#EEEEEE"
    android:inputType="textMultiLine"
    android:maxLines="5"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="4dp"
    android:paddingBottom="4dp"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:shadowColor="#000000"
    android:shadowRadius="3"
    android:shadowDx="3"
    android:shadowDy="3"/> 

</RelativeLayout>

Wow, sorry but I came up with a theory, tested it, and realized the answer immediately after posting the question.

Using findViewById() on an include tag's id returns the root view of the included file.

Tested it using:

input_box_wrapper = findViewById( R.id.input_box_layout );

if ( input_box_wrapper == null )
    Log.i( "another", "it's null" );
else
{
    Log.i("another", "not null");

    if ( input_box_wrapper instanceof RelativeLayout )
        Log.i("another", "it's the root view");  
}

Output:

not null

it's the root view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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