zoukankan      html  css  js  c++  java
  • 【原创】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 }
    
    
    
     
    这样也能达到同样的效果 ,不过发现个问题,控件的位置不好控制,留待以后吧。。。。
    作者:leemoaly
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    [Go] 解决packets.go:36: read tcp 127.0.0.1:51139->127.0.0.1:3306: wsarecv: An established connection was aborted by the software in your host ma chine.
    [javascript] js格式化时间为xx秒前、xx分钟前、xx小时前等
    [日常]UserAgent中的AhrefsBot解释
    [PHP] laravel 框架多项目公用redis会有默认前缀 项目名_database_key
    [Go] go run 时 os.Getwd 和 os.Executable 获取程序根路径
    [laravel] 关闭laravel数据库model维护create_at update_at
    [Go]go.mod 文件中的// indirect意思代表间接依赖
    [Go] 解决 imported from implicitly required module
    [mysql] 解决SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column
    [PHP] windows环境下PHP增加rdkafka扩展 解决需要ext-rdkafka问题
  • 原文地址:https://www.cnblogs.com/kavs/p/4433506.html
Copyright © 2011-2022 走看看