综合之前的Fragment和自定义组件的知识,实现微信界面

MainActivity.java

  1. package cn.lixyz.test;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Window;
  6. import android.widget.RadioGroup;
  7. import android.widget.RadioGroup.OnCheckedChangeListener;
  8. import cn.lixyz.test.fragment.DiscoverFragment;
  9. import cn.lixyz.test.fragment.MeFragment;
  10. import cn.lixyz.test.fragment.TXLFragment;
  11. import cn.lixyz.test.fragment.WeChatFragment;
  12.  
  13. public class MainActivity extends Activity implements OnCheckedChangeListener {
  14.  
  15. private RadioGroup rg_tab_buttons;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. requestWindowFeature(Window.FEATURE_NO_TITLE);
  21. setContentView(R.layout.activity_main);
  22.  
  23. getFragmentManager().beginTransaction().add(R.id.layout, new WeChatFragment(), "wechat").commit();
  24. rg_tab_buttons = (RadioGroup) findViewById(R.id.rg_tab_buttons);
  25. rg_tab_buttons.setOnCheckedChangeListener(this);
  26.  
  27. }
  28.  
  29. @Override
  30. public void onCheckedChanged(RadioGroup group, int checkedId) {
  31. switch (checkedId) {
  32. case R.id.rb_wechat:
  33. getFragmentManager().beginTransaction().replace(R.id.layout, new WeChatFragment(), "wechat").commit();
  34. break;
  35. case R.id.rb_txl:
  36. getFragmentManager().beginTransaction().replace(R.id.layout, new TXLFragment(), "txl").commit();
  37. break;
  38. case R.id.rb_discover:
  39. getFragmentManager().beginTransaction().replace(R.id.layout, new DiscoverFragment(), "discover").commit();
  40. break;
  41. case R.id.rb_me:
  42. getFragmentManager().beginTransaction().replace(R.id.layout, new MeFragment(), "me").commit();
  43. break;
  44. }
  45. }
  46.  
  47. }

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#f9f9f9"
  6. android:orientation="vertical" >
  7.  
  8. <LinearLayout
  9. android:id="@+id/layout"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:orientation="vertical" >
  14. </LinearLayout>
  15.  
  16. <RadioGroup
  17. android:id="@+id/rg_tab_buttons"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="horizontal" >
  21.  
  22. <TextView
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="1" />
  26.  
  27. <RadioButton
  28. android:id="@+id/rb_wechat"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:background="@drawable/wechat_icon"
  32. android:button="@null" />
  33.  
  34. <TextView
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="1" />
  38.  
  39. <RadioButton
  40. android:id="@+id/rb_txl"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:background="@drawable/txl_icon"
  44. android:button="@null" />
  45.  
  46. <TextView
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_weight="1" />
  50.  
  51. <RadioButton
  52. android:id="@+id/rb_discover"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:background="@drawable/discover_icon"
  56. android:button="@null" />
  57.  
  58. <TextView
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:layout_weight="1" />
  62.  
  63. <RadioButton
  64. android:id="@+id/rb_me"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:background="@drawable/me_icon"
  68. android:button="@null" />
  69.  
  70. <TextView
  71. android:layout_width="wrap_content"
  72. android:layout_height="wrap_content"
  73. android:layout_weight="1" />
  74. </RadioGroup>
  75.  
  76. </LinearLayout>

DiscoverFragment.java

  1. package cn.lixyz.test.fragment;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import cn.lixyz.test.R;
  9.  
  10. public class DiscoverFragment extends Fragment {
  11.  
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  14. return inflater.inflate(R.layout.discover_fragment, container, false);
  15. }
  16.  
  17. }

discover_fragment.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7.  
  8. <cn.lixyz.test.view.MyCustomTitleBar
  9. android:layout_width="wrap_content"
  10. android:layout_height="33dp"
  11. custom:titleText="发现"
  12. custom:titleTextColor="#ffffff"
  13. custom:titleTextSize="5sp" />
  14.  
  15. </LinearLayout>

MeFragment.java

  1. package cn.lixyz.test.fragment;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import cn.lixyz.test.R;
  9.  
  10. public class MeFragment extends Fragment {
  11.  
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  14. return inflater.inflate(R.layout.me_fragment, container, false);
  15. }
  16.  
  17. }

me_fragment.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7.  
  8. <cn.lixyz.test.view.MyCustomTitleBar
  9. android:layout_width="wrap_content"
  10. android:layout_height="33dp"
  11. custom:titleText="我"
  12. custom:titleTextColor="#ffffff"
  13. custom:titleTextSize="5sp" />
  14.  
  15. </LinearLayout>

TXLFragment.java

  1. package cn.lixyz.test.fragment;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import cn.lixyz.test.R;
  9.  
  10. public class TXLFragment extends Fragment {
  11.  
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  14. return inflater.inflate(R.layout.txl_fragment, container, false);
  15. }
  16.  
  17. }

txl_fragment.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7.  
  8. <cn.lixyz.test.view.MyCustomTitleBar
  9. android:layout_width="wrap_content"
  10. android:layout_height="33dp"
  11. custom:titleButtonImage="@drawable/image"
  12. custom:titleText="通讯录"
  13. custom:titleTextColor="#ffffff"
  14. custom:titleTextSize="5sp" />
  15.  
  16. </LinearLayout>

WeChatFragment.java

  1. package cn.lixyz.test.fragment;
  2.  
  3. import android.app.Fragment;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import cn.lixyz.test.R;
  9.  
  10. public class WeChatFragment extends Fragment {
  11.  
  12. @Override
  13. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  14. return inflater.inflate(R.layout.wechat_fragment, container, false);
  15. }
  16.  
  17. }

wechat_fragment.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7.  
  8. <cn.lixyz.test.view.MyCustomTitleBar
  9. android:layout_width="wrap_content"
  10. android:layout_height="33dp"
  11. custom:titleButtonImage="@drawable/plus"
  12. custom:titleText="微信"
  13. custom:titleTextColor="#ffffff"
  14. custom:titleTextSize="5sp" />
  15.  
  16. </LinearLayout>

MyCustomTitleBar.java

  1. package cn.lixyz.test.view;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.graphics.drawable.Drawable;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9. import android.widget.ImageButton;
  10. import android.widget.RelativeLayout;
  11. import android.widget.TextView;
  12. import cn.lixyz.test.R;
  13.  
  14. @SuppressLint("NewApi")
  15. public class MyCustomTitleBar extends RelativeLayout {
  16.  
  17. // 定义自定义控件包含的组件
  18. private TextView title;
  19. private ImageButton button;
  20.  
  21. // 定义控件的属性
  22. private String titleText;
  23. private float titleTextSize;
  24. private int titleTextColor;
  25. private Drawable titleButtonImage;
  26.  
  27. // 为每个控件定义LayoutParams
  28. private LayoutParams textLayoutParams;
  29. private LayoutParams buttonLayoutParams;
  30.  
  31. private customTitleBarListener listener;
  32.  
  33. public interface customTitleBarListener {
  34. public void click();
  35. }
  36.  
  37. public void setCustomTitleBarListener(customTitleBarListener listener) {
  38. this.listener = listener;
  39. }
  40.  
  41. public MyCustomTitleBar(Context context, AttributeSet attrs) {
  42. super(context, attrs);
  43.  
  44. // 获取我们定义的属性
  45. TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyCustomTitleBar);
  46.  
  47. titleText = array.getString(R.styleable.MyCustomTitleBar_titleText);
  48. titleTextColor = array.getColor(R.styleable.MyCustomTitleBar_titleTextColor, 0);
  49. titleTextSize = array.getDimension(R.styleable.MyCustomTitleBar_titleTextSize, 10);
  50. titleButtonImage = array.getDrawable(R.styleable.MyCustomTitleBar_titleButtonImage);
  51.  
  52. // 回收,以防出错
  53. array.recycle();
  54.  
  55. // 新建包含的子组件
  56. title = new TextView(context);
  57. button = new ImageButton(context);
  58.  
  59. // 为子组件赋值
  60. title.setText(titleText);
  61. title.setTextColor(titleTextColor);
  62. title.setTextSize(titleTextSize);
  63. button.setBackground(titleButtonImage);
  64.  
  65. // 设置背景色
  66. setBackgroundColor(0xFF38373c);
  67.  
  68. // 设置包含控件的属性并添加到view中
  69. textLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  70. textLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
  71. addView(title, textLayoutParams);
  72. buttonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  73. buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  74. addView(button, buttonLayoutParams);
  75.  
  76. button.setOnClickListener(new View.OnClickListener() {
  77.  
  78. @Override
  79. public void onClick(View v) {
  80. listener.click();
  81. }
  82. });
  83.  
  84. }
  85. }

attrs.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <declare-styleable name="MyCustomTitleBar">
  5. <attr name="titleText" format="string" />
  6. <attr name="titleTextSize" format="dimension" />
  7. <attr name="titleTextColor" format="color" />
  8. <attr name="titleButtonImage" format="reference" />
  9. </declare-styleable>
  10.  
  11. </resources>

Android笔记(六十九) 仿微信界面(一)的更多相关文章

  1. Android笔记(六十八) Fragment总结

    Fragment的产生: 为了适应各种尺寸的屏幕,谷歌推出Fragment,可以把Fragment成Activity的一个组成部分,它拥有自己的生命周期.可以接收并处理用户的各种事件,还可以动态的增删 ...

  2. Android笔记(十九) Android中的Fragment

    通常我们使用Activity来展示界面,但是在手机上界面可能显示的很好看,但在平板上,因为平板的屏幕非常大,手机的界面放在平板上可能会出现控件被拉长.控件之间间距变大等问题.为了更好的体验效果,在Ac ...

  3. Android笔记(六十六) android中的动画——XML文件定义属性动画

    除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...

  4. Android笔记(六十五) android中的动画——属性动画(propertyanimation)

    补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...

  5. Android笔记(六十四) android中的动画——补间动画(tweened animation)

    补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...

  6. Android笔记(六十二)网络框架volley

    什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...

  7. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  8. Android ActionBar仿微信界面

    ActionBar仿微信界面 1.学习了别人的两篇关于ActionBar博客,在结合别人的文章来仿造一下微信的界面: 思路如下:1).利用ActionBar生成界面的头部,在用ActionBar的Ac ...

  9. 转-Fragment+ViewPager组件(高仿微信界面)

    http://www.cnblogs.com/lichenwei/p/3982302.html 什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开 ...

随机推荐

  1. jenkins报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) 的处理

    问题背景:jenkins服务器发布代码后需要执行删除缓存的操作ssh -p222 eus_pe_devadmin@1.1.1.1 "sudo rm -rf /dev/shm/nginx/hi ...

  2. redis连接池——JedisPool和JedisCluster的介绍与使用

    目录 Jedis使用方式的介绍 Redis连接池介绍 创建连接池配置文件 单机版的Redis连接池 集群版的Redis连接池 总结 Jedis使用方式的介绍 Jedis就是Java实现的操作Redis ...

  3. 2-5 【ngFor指令 事件的处理和样式绑定】顶部导航支持选中状态

    索引的获取 first和last是布尔类型的 奇数偶数 []方括号表示的是数据绑定 ()圆括号就是事件绑定 . 开始代码 我们要实现的功能是菜单点击后,就变色 这样我们就得到了索引. 新建一个clas ...

  4. [译]如何比较master分支上与git上任意的一个老版本的区别?

    原文来源:https://stackoverflow.com/questions/5586383/how-to-diff-one-file-to-an-arbitrary-version-in-git ...

  5. matlab学习笔记10_3关系运算符和逻辑运算符

    一起来学matlab-matlab学习笔记10 10_3关系运算符和逻辑运算符 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰 ...

  6. linux中安装robot环境

    https://www.cnblogs.com/lgqboke/p/8252488.html(文中验证robotframework命令应该为 robot --version) 可能遇到的问题: 1.p ...

  7. 机器学习技法总结(一):支持向量机(linear support vector machine,dual support vector machine)

    第一阶段技法: large margin (the relationship between large marin and regularization), hard-SVM,soft-SVM,du ...

  8. 在ensp上的进行的浮动路由

    原理 实验模拟内容 搭建实验拓扑 相关参数 其他设置端口ip都一样,serial也一样(三个路由器都要设置的) 简单测试一下连通性 下面我们分别在路由器上配置所在网段的静态路由 配置完之后我们来查看一 ...

  9. Educational Codeforces Round 71

    https://www.cnblogs.com/31415926535x/p/11460682.html 上午没课,做一套题,,练一下手感和思维,,教育场的71 ,,前两到没啥,,后面就做的磕磕巴巴的 ...

  10. Word样式教程

    目录 写在前面 样式可以解决什么问题? 本文适合于 快速入门 一切皆样式 样式与格式的关系 如何修改样式 建立新的样式 样式的匹配和更新 根据样式更新所选段落 根据所选段落更新样式 小结 进一步了解 ...