android – 如何从膨胀的布局访问视图?

我有一个简单的Activity附加了main.xml. main.xml有一个自定义布局(customLinearLayout)和一个简单的TextView.自定义布局还有自己的布局文件(linearlayout.xml)和一个简单的Button.布局由班级正确夸大.

linearlayout.xml中的Button应该更改main.xml中textView的文本,但是我无法访问该textView.我究竟做错了什么?我也不能给main.xml充气.

这是customLinearLayout:

public class CustomLinearLayout extends LinearLayout {

LayoutInflater mInflater;
View ContainerView;

Button button1;
TextView tv;

public CustomLinearLayout(Context context) {
    super(context);
    // Todo Auto-generated constructor stub
    init();
}

public CustomLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    // Todo Auto-generated constructor stub
    init();
}

private void init() {
    mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ContainerView = mInflater.inflate(R.layout.linearlayout, this, true);

    button1 = (Button) ContainerView.findViewById(R.id.button1);

    // trying to get the TextView from main.xml
    tv = (TextView) ContainerView.findViewById(R.id.textview1); // doesn't work

    // these tries doesn't work either
    //tv = (TextView) ContainerView.getRootView().findViewById(R.id.textview);  
    //tv = (TextView) this.getRootView().findViewById(R.id.textview);
    //tv = (TextView) this.findViewById(R.id.textview);

    button1.setonClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            // Todo Auto-generated method stub
            if(tv != null)
                tv.setText("works");
            else
                Log.d("CustomLinearLayout", "TextView not found");
        }
    });
}
}

layout-file(linearlayout.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" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="press" />

</LinearLayout>

活动:

public class TestLayoutViewsv2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

Activity的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<test.layoutviewsv2.CustomLinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

解决方法:

方法findViewById是作用域的 – 您的自定义视图在内部查找不存在的文本字段.但那没关系!你的观点应该是他们自己的小世界 – 他们不应该对外面的世界了解任何事情.可悲的是,你将不得不改变你的设计以适应这种情况(比如对活动的回调,也许)让你做你想做的事.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...