简体   繁体   English

包含 xml 布局的访问视图

[英]Access view of included xml layout

I need to access the view present in a.xml layout included in another b.xml layout.我需要访问另一个 b.xml 布局中包含的 a.xml 布局中的视图。 For example, This is a.xml例如,这是 a.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/xyz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XYZ" />
</RelativeLayout>

And, in b.xml并且,在 b.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <include
        android:id="@+id/a_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout="@layout/a" />

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/xyz"
        android:text="Show me below xyz" />
</RelativeLayout>

I have to do it in xml code, because if I do it in Java it must be after setContentView() and then setting LayoutParams of TextView 'label' wont take effect.我必须在 xml 代码中这样做,因为如果我在 Java 中这样做,它必须在 setContentView() 之后,然后设置 TextView 'label' 的 LayoutParams 不会生效。

I guess, everyone understand what I am trying to ask.我想,每个人都明白我想问什么。 Waiting for good replies.等待好的答复。

Thanks all.谢谢大家。

Image on the right is what I am trying to achieve and left one is what I am getting with the current code.右边的图片是我想要实现的,左边的是我用当前代码得到的。

这就是我用当前的 xml 代码得到的这就是我想要做的

In b.xml, you should correct:在 b.xml 中,您应该更正:

android:layout_below="@id/xyz"

to

android:layout_below="@id/a_layout"

And then you can use it in your code (Here I place it in onCreate):然后你可以在你的代码中使用它(这里我把它放在 onCreate 中):

setContentView(R.layout.b);    
((Button)findViewById(R.id.xyz)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ((TextView)findViewById(R.id.label)).setText("clicked on XYZ button");
        }
    });

The problem is not in accessing the View of included layout, but in the fact that you cannot achieve this layouts "overlapping".问题不在于访问包含布局的View ,而在于您无法实现此布局“重叠”。 Let me explain: if you add some more views below the button in your a.xml and then try to place some view in your b.xml just below the button then it would make the views from b.xml overlap views from a.xml , however this has not been implemented in Android (yet?).让我解释一下:如果您在a.xml中的按钮下方添加更多视图,然后尝试在按钮正下方的b.xml中放置一些视图,那么它会使a.xml中的视图与b.xml中的视图重叠,但是这还没有在 Android 中实现(还没有?)。 So, the only thing you can do is to put android:layout_below="@id/a_layout" as @Hoàng Toản suggested.所以,你唯一能做的就是按照@Hoàng Toản 的建议放置android:layout_below="@id/a_layout"

PS You may observe same behavior with this a + b layouts combination: PS 你可能会观察到这个a + b布局组合的相同行为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/xyz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="XYZ" />
    </RelativeLayout>

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/xyz"
        android:text="Show me below xyz" />
</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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