包含的XML布局视图访问 [英] Access view of included xml layout

查看:126
本文介绍了包含的XML布局视图访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问a.xml布局视图present包括在另一b.xml布局。
例如,
这是a.xml

I need to access the view present in a.xml layout included in another b.xml layout. For example, This is 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>

和,在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>

我必须这样做,在XML code,因为如果我在Java中做到这一点就必须经过的setContentView()和TextView的标签,那么设置的LayoutParams不会生效。

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.

我想,每个人都明白我想问问。等待好答复。

I guess, everyone understand what I am trying to ask. Waiting for good replies.

谢谢大家。

在右侧的图片是什么,我想实现和左边的一个是我当前code得到。

Image on the right is what I am trying to achieve and left one is what I am getting with the current code.


推荐答案

在b.xml,您应该纠正:

In b.xml, you should correct:

android:layout_below="@id/xyz"

android:layout_below="@id/a_layout"

然后你就可以在你的code使用它(在这里我把它放在的onCreate):

And then you can use it in your code (Here I place it in 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");
        }
    });

这篇关于包含的XML布局视图访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆