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

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 }
 
 

 

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

转载于:https://www.cnblogs.com/kavs/p/4433506.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值