码迷,mamicode.com
首页 > 移动开发 > 详细

【原创】Android多个xml文件的使用

时间:2015-04-16 23:41:12      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

Android中经常会使用多个xml文件,但在Mainactivity中使用的setContentView(R.layout.main)只加载main.xml文件,其他xml文件不加载进当前视图,当我们要用到其他xml文件中的控件是发现直接使用findViewById()方法时不报错但控件的值找不到为null,而一旦为该控件添加相应事件就会出现空指针异常。原因就在于控件并未加载进当前视图。

解决方法:两种

1、使用在main.xml中使用include语句

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

2、使用LayoutInflater 举个简单;例子

两个xml文件main.xml和x.xml

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" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>

x.xml

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

   <Button 
       android:id="@+id/bt"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       
       android:layout_below="@id/tv"
       />

</RelativeLayout>

 

activity中的代码:

 


 1 package leemo.e;
 2 
 3 
 4 import android.app.Activity;
 5 import android.os.Bundle;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.LinearLayout;
11 import android.widget.TextView;
12 import android.widget.Toast;
13 
14 public class EeeActivity extends Activity {
15     /** Called when the activity is first created. */
16     @Override
17     public void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.main);
20         TextView tv = (TextView) findViewById(R.id.tv);
21         tv.setText("content is change");
22         
23         
24         LayoutInflater layout=this.getLayoutInflater();
25         View view=layout.inflate(R.layout.x,null);
26         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
27                 LinearLayout.LayoutParams.FILL_PARENT,  
28                 LinearLayout.LayoutParams.WRAP_CONTENT);
29                 addContentView(view,params);
30         Button bt = (Button) view.findViewById(R.id.bt);
31         bt.setText("µã»÷ÎÒ ");
32         bt.setOnClickListener(new OnClickListener() {
33 
34             @Override
35             public void onClick(View v) {
36                 // TODO Auto-generated method stub
37                 Toast.makeText(EeeActivity.this, "button click",
38                         Toast.LENGTH_SHORT).show();
39             }
40         });
41         
42     }
43     
44 }

 

 
这样也能达到同样的效果 ,不过发现个问题,控件的位置不好控制,留待以后吧。。。。

【原创】Android多个xml文件的使用

标签:

原文地址:http://www.cnblogs.com/kavs/p/4433506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!