New UI-<include>标签解决布局重用问题

New UI-<include>标签解决布局重用问题

 ——转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途!


小猪Android开发交流群已建立,欢迎大家加入,无论是新手,菜鸟,大神都可以,小猪一个人的

力量毕竟是有限的,写出来的东西肯定会有很多纰漏不足,欢迎大家指出,集思广益,让小猪的博文

更加的详尽,帮到更多的人,O(∩_∩)O谢谢!

小猪Android开发交流群:小猪Android开发交流群群号:421858269

新Android UI实例大全目录:http://blog.csdn.net/coder_pig/article/details/42145907



如果你已经知道include是什么,只是想知道怎么用,使用示例如下:

①布局文件引入layout 

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <include  
  2.         android:id="@+id/topbar"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         layout="@layout/view_topbar" />  
②Java代码中访问,获取layout中组件,设置属性:

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. private View topbar;  
  2. private TextView txtTitle;  
  3.   
  4. topbar = findViewById(R.id.topbar);  
  5. txtTitle = (TextView) topbar.findViewById(R.id.txtTitle);  
  6. txtTitle.setText("第二页");  

就这么简单,你可以关页面了,觉得帮助了你,可以点个赞!大笑


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


如果不知道,想了解相关以及应用场景请继续看:

本节流程图:




效果图:




代码思路:

核心是顶部的一个titlebar,这里是我们自己写的一个bar,因为后面两个Activity都需要用到,

所以我们直接通过include包含这个bar,然后Java代码中获得该bar实例,再调用bar.findViewById

找到标题栏的TextView修改对应的页面!



详细代码如下:

顶部的bar: view_topbar.xml

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#000000" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/imgBack"  
  9.         android:layout_width="40dp"  
  10.         android:layout_height="40dp"  
  11.         android:background="@drawable/back" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/txtTitle"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_centerHorizontal="true"  
  18.         android:layout_marginTop="8dp"  
  19.         android:textColor="#FFFFFF"  
  20.         android:textSize="20sp" />  
  21.   
  22. </RelativeLayout>  

接着依次编写三个Activity的布局,二三个activity通过include导入topbar

activity_main.xml:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#D1E5E6"  
  6.     tools:context="com.jay.example.includedemo.MainActivity" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/btnTo"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerInParent="true"  
  13.         android:text="跳轉到第二個頁面" />  
  14.   
  15. </RelativeLayout>  

activity_second.xml:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#D5E094" >  
  6.   
  7.     <include  
  8.         android:id="@+id/topbar"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         layout="@layout/view_topbar" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/btnTo"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_centerInParent="true"  
  18.         android:text="跳轉到第三個頁面"  
  19.         android:textSize="14sp" />  
  20.   
  21. </RelativeLayout>  

activity_third.xml:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#F2CC9F" >  
  6.   
  7.     <include  
  8.         android:id="@+id/topbar"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         layout="@layout/view_topbar" />  
  12.   
  13.     <TextView  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_centerInParent="true"  
  17.         android:text="第三個頁面" />  
  18.   
  19. </RelativeLayout>  


编写对应的activity:

MainActivity.java:

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.includedemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     private Button btnTo;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         getViews();  
  18.         setViews();  
  19.     }  
  20.   
  21.     private void getViews() {  
  22.         btnTo = (Button) findViewById(R.id.btnTo);  
  23.     }  
  24.   
  25.     private void setViews() {  
  26.         MyClick myClick = new MyClick();  
  27.         btnTo.setOnClickListener(myClick);  
  28.   
  29.     }  
  30.   
  31.     // 定义事件处理类  
  32.     private class MyClick implements OnClickListener {  
  33.         @Override  
  34.         public void onClick(View v) {  
  35.             switch (v.getId()) {  
  36.             case R.id.btnTo:  
  37.                 Intent it = new Intent(MainActivity.this, SecondActivity.class);  
  38.                 startActivity(it);  
  39.                 break;  
  40.             }  
  41.   
  42.         }  
  43.     }  
  44. }  

SecondActivity.java:

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.includedemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.ImageView;  
  10. import android.widget.TextView;  
  11.   
  12. public class SecondActivity extends Activity {  
  13.     private View topbar;  
  14.     private ImageView imgBack;  
  15.     private TextView txtTitle;  
  16.     private Button btnTo;  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_second);  
  22.         getActionBar().hide();  
  23.         getViews();  
  24.         setViews();  
  25.     }  
  26.   
  27.     private void getViews() {  
  28.         topbar = findViewById(R.id.topbar);  
  29.         btnTo = (Button) findViewById(R.id.btnTo);  
  30.         imgBack = (ImageView) topbar.findViewById(R.id.imgBack);  
  31.         txtTitle = (TextView) topbar.findViewById(R.id.txtTitle);  
  32.     }  
  33.   
  34.     private void setViews() {  
  35.         MyClick myClick = new MyClick();  
  36.         txtTitle.setText("第二页");  
  37.         btnTo.setOnClickListener(myClick);  
  38.         imgBack.setOnClickListener(myClick);  
  39.     }  
  40.   
  41.     // 定义事件处理类  
  42.     private class MyClick implements OnClickListener {  
  43.         @Override  
  44.         public void onClick(View v) {  
  45.             switch (v.getId()) {  
  46.             case R.id.btnTo:  
  47.                 Intent it = new Intent(SecondActivity.this, ThirdActivity.class);  
  48.                 startActivity(it);  
  49.                 break;  
  50.             case R.id.imgBack:  
  51.                 finish();  
  52.                 break;  
  53.             }  
  54.   
  55.         }  
  56.     }  
  57. }  

ThirdActivity.java:

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.includedemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.ImageView;  
  8. import android.widget.TextView;  
  9.   
  10. public class ThirdActivity extends Activity {  
  11.     private View topbar;  
  12.     private ImageView imgBack;  
  13.     private TextView txtTitle;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_third);  
  19.         getActionBar().hide();  
  20.         getViews();  
  21.         setViews();  
  22.     }  
  23.   
  24.     private void getViews() {  
  25.         topbar = findViewById(R.id.topbar);  
  26.         imgBack = (ImageView) topbar.findViewById(R.id.imgBack);  
  27.         txtTitle = (TextView) topbar.findViewById(R.id.txtTitle);  
  28.     }  
  29.   
  30.     private void setViews() {  
  31.         MyClick myClick = new MyClick();  
  32.         txtTitle.setText("第三页");  
  33.         imgBack.setOnClickListener(myClick);  
  34.     }  
  35.   
  36.     // 定义事件处理类  
  37.     private class MyClick implements OnClickListener {  
  38.         @Override  
  39.         public void onClick(View v) {  
  40.             switch (v.getId()) {  
  41.             case R.id.imgBack:  
  42.                 finish();  
  43.                 break;  
  44.             }  
  45.   
  46.         }  
  47.     }  
  48. }  


最后再配置文件注册下二三两个Activity即可!

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <activity android:name=".SecondActivity"></activity>  
  2.      <activity android:name=".ThirdActivity"></activity>  



代码下载:

http://pan.baidu.com/s/1bnpjJhP

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值