zoukankan      html  css  js  c++  java
  • 使用include实现布局(layout)复用

    http://www.ourunix.org/android/post/87.html

         假使我们遇到这么一种情况,我们需要开发一个app,这个app的基本所有的Activity都使用到了相同的布局,我们该如何设计?我们是给这些个Activity布局文件都统一加上一样的布局代码吗?但是这样维护起来很麻烦,修改很不方便,Android布局中有没有一种类似于编程语言的include语法呢?答案是有的,但是sdk的demo并没有说出使用方法,但这并不说明不好使用,其实很简单。下面的IncludeXmlTest工程给出了样式。

            我们新建一个IncludeXmlTest工程,我们先从布局文件上下手,我们新建一个真正的real.xml文件,来实现我们的布局,代码如下:

    XML/HTML代码
    1. <?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/button"  
              android:layout_width="wrap_content"  
              android:layout_height="wrap_content"  
              android:text="show" />  
        
          <TextView  
              android:id="@+id/text"  
              android:layout_width="wrap_content"  
              android:layout_height="wrap_content"  
              android:text="" />  
        
      </LinearLayout>  

            然后在我们需要引入的xml文件中,include这个real.xml就可以了,我这个main.xml代码如下:

    XML/HTML代码
    1. <?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" >  
        
         <include   
             android:id="@+id/include"  
             layout="@layout/real"/>  
        
      </LinearLayout>  

            这个include部分就是引入了real.xml的布局,之后我们在程序中只需要布局main.xml文件即可:

    Java代码
    1. public class IncludeXmlTestActivity extends Activity {  
          private TextView mTextView;  
          private Button mButton;  
            
          /** Called when the activity is first created. */  
          @Override  
          public void onCreate(Bundle savedInstanceState) {  
              super.onCreate(savedInstanceState);  
              setContentView(R.layout.main);  
                
              initView();  
          }  
            
          /** 
           * 设置view 
           * */  
          public void initView(){  
              mTextView = (TextView) findViewById(R.id.text);  
              mButton = (Button)findViewById(R.id.button);  
              mButton.setOnClickListener(new OnClickListener() {  
                    
                  @Override  
                  public void onClick(View v) {  
                      // TODO Auto-generated method stub  
                      mTextView.setText("hello, i am here");  
                  }  
              });  
          }  
      }  

            怎么样简单吧,大家在重复布局的时候一定要记住使用哦,最后看下运行效果:

    运行程序

    点击按钮,显示textView的文字

            源码下载:使用include实现布局(layout)复用

  • 相关阅读:
    mysql 的安装
    nginx的安装
    修改网站默认目录
    配置yum仓库 安装httpd服务
    安装 VMware Tools
    phpstrom + xdebug 断点调试
    公网IP访问服务器
    mysql in操作和find_in_set函数
    网页授权有时候获取不到openid 的坑
    解决Required Integer parameter 'id' is not present的一种方法
  • 原文地址:https://www.cnblogs.com/shuiyun/p/2965717.html
Copyright © 2011-2022 走看看