Android layout 如何声明包含在“包含”布局中的变量?

Android layout 如何声明包含在“包含”布局中的变量?,android-layout,Android Layout,我的主布局和次布局包含变量。我将次要布局包括在主要布局中。既然次要布局是自定义的xml,我如何访问和声明MainActivity中次要布局中的变量 我发现很难理解数据绑定的动态 <RelativeLayout android:layout_width="match_parent" android:layout_height="150dp" android:background="#75c49b"> <ImageV

我的主布局和次布局包含变量。我将次要布局包括在主要布局中。既然次要布局是自定义的xml,我如何访问和声明MainActivity中次要布局中的变量

我发现很难理解数据绑定的动态

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#75c49b">

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_alignBottom="@+id/textView15"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:src="@drawable/pp" />

        <TextView
            android:id="@+id/changePhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/imageView5"
            android:layout_alignRight="@+id/imageView5"
            android:layout_below="@+id/imageView5"
            android:text="Change Photo"
            android:textColor="@color/colorAccent" />

        <TextView
            android:id="@+id/textView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="57dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_toEndOf="@+id/imageView5"
            android:layout_toRightOf="@+id/imageView5"
            android:text="User ID:"
            android:textColor="@color/colorAccent" />



        <TextView
            android:id="@+id/textView16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView15"
            android:layout_alignStart="@+id/textView15"
            android:layout_below="@+id/logOut"
            android:layout_marginTop="8dp"
            android:text="Welcome"
            android:textColor="@color/colorAccent"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/getFirstName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView16"
            android:layout_alignBottom="@+id/textView16"
            android:layout_marginLeft="9dp"
            android:layout_marginStart="9dp"
            android:layout_toEndOf="@+id/textView16"
            android:layout_toRightOf="@+id/textView16"
            android:text="Baba"
            android:textColor="@color/colorAccent"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/logOut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/imageView5"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="9dp"
            android:text="Logout"
            android:textColor="@color/colorAccent" />


    </RelativeLayout>


    <ScrollView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="false"
        android:fadingEdge="none"
        android:fillViewport="true"
        android:overScrollMode="never"
        android:scrollbars="none"
        android:scrollingCache="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include layout="@layout/themenugrid"> </include>

我想访问自定义.xml文件themenugrid中的变量,因为我已经将它们包含在主布局中

您可以使用.xml文件中的findviewbyd直接访问include布局的Id。因为包含布局是父布局的一部分。不需要做额外的代码或任何事情

检查下面的示例

TextView tvName = findViewById(R.id.tv_name);
tvName.setText("Your Include layout ID access and set Text in this textView");
喜欢

themenugrid.xml


只需像访问xml中的任何其他元素一样访问它。 在您的案例中,写下:

 TextView textView = findViewById(R.id.sample_text_view);
 textView.setText("Sample 2");

所有包含的布局都将成为父布局的一部分。您可以通过findViewBy Id.ie访问

 View textView = findViewById(R.id.id_inclued_view);
 textView.setText("My Name is Khan. And I am not terrorist :P");

所有包含的布局都将成为父布局的一部分。您可以通过FindView Id方法访问。非常感谢。这真的很有帮助。非常感谢考沙尔。我现在好了。
 View header = findViewById(R.id.header_layout);
 TextView textView = header.findViewById(R.id.sample_text_view);
 textView.setText("Sample 2");
 TextView textView = findViewById(R.id.sample_text_view);
 textView.setText("Sample 2");
 View textView = findViewById(R.id.id_inclued_view);
 textView.setText("My Name is Khan. And I am not terrorist :P");