Android笔记(六十九) 仿微信界面(一)
综合之前的Fragment和自定义组件的知识,实现微信界面
MainActivity.java
- package cn.lixyz.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Window;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- import cn.lixyz.test.fragment.DiscoverFragment;
- import cn.lixyz.test.fragment.MeFragment;
- import cn.lixyz.test.fragment.TXLFragment;
- import cn.lixyz.test.fragment.WeChatFragment;
- public class MainActivity extends Activity implements OnCheckedChangeListener {
- private RadioGroup rg_tab_buttons;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- getFragmentManager().beginTransaction().add(R.id.layout, new WeChatFragment(), "wechat").commit();
- rg_tab_buttons = (RadioGroup) findViewById(R.id.rg_tab_buttons);
- rg_tab_buttons.setOnCheckedChangeListener(this);
- }
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- switch (checkedId) {
- case R.id.rb_wechat:
- getFragmentManager().beginTransaction().replace(R.id.layout, new WeChatFragment(), "wechat").commit();
- break;
- case R.id.rb_txl:
- getFragmentManager().beginTransaction().replace(R.id.layout, new TXLFragment(), "txl").commit();
- break;
- case R.id.rb_discover:
- getFragmentManager().beginTransaction().replace(R.id.layout, new DiscoverFragment(), "discover").commit();
- break;
- case R.id.rb_me:
- getFragmentManager().beginTransaction().replace(R.id.layout, new MeFragment(), "me").commit();
- break;
- }
- }
- }
activity_main.xml
- <?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:background="#f9f9f9"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/layout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:orientation="vertical" >
- </LinearLayout>
- <RadioGroup
- android:id="@+id/rg_tab_buttons"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- <RadioButton
- android:id="@+id/rb_wechat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/wechat_icon"
- android:button="@null" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- <RadioButton
- android:id="@+id/rb_txl"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/txl_icon"
- android:button="@null" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- <RadioButton
- android:id="@+id/rb_discover"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/discover_icon"
- android:button="@null" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- <RadioButton
- android:id="@+id/rb_me"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/me_icon"
- android:button="@null" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- </RadioGroup>
- </LinearLayout>
DiscoverFragment.java
- package cn.lixyz.test.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import cn.lixyz.test.R;
- public class DiscoverFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.discover_fragment, container, false);
- }
- }
discover_fragment.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <cn.lixyz.test.view.MyCustomTitleBar
- android:layout_width="wrap_content"
- android:layout_height="33dp"
- custom:titleText="发现"
- custom:titleTextColor="#ffffff"
- custom:titleTextSize="5sp" />
- </LinearLayout>
MeFragment.java
- package cn.lixyz.test.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import cn.lixyz.test.R;
- public class MeFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.me_fragment, container, false);
- }
- }
me_fragment.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <cn.lixyz.test.view.MyCustomTitleBar
- android:layout_width="wrap_content"
- android:layout_height="33dp"
- custom:titleText="我"
- custom:titleTextColor="#ffffff"
- custom:titleTextSize="5sp" />
- </LinearLayout>
TXLFragment.java
- package cn.lixyz.test.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import cn.lixyz.test.R;
- public class TXLFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.txl_fragment, container, false);
- }
- }
txl_fragment.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <cn.lixyz.test.view.MyCustomTitleBar
- android:layout_width="wrap_content"
- android:layout_height="33dp"
- custom:titleButtonImage="@drawable/image"
- custom:titleText="通讯录"
- custom:titleTextColor="#ffffff"
- custom:titleTextSize="5sp" />
- </LinearLayout>
WeChatFragment.java
- package cn.lixyz.test.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import cn.lixyz.test.R;
- public class WeChatFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- return inflater.inflate(R.layout.wechat_fragment, container, false);
- }
- }
wechat_fragment.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:custom="http://schemas.android.com/apk/res/cn.lixyz.test"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <cn.lixyz.test.view.MyCustomTitleBar
- android:layout_width="wrap_content"
- android:layout_height="33dp"
- custom:titleButtonImage="@drawable/plus"
- custom:titleText="微信"
- custom:titleTextColor="#ffffff"
- custom:titleTextSize="5sp" />
- </LinearLayout>
MyCustomTitleBar.java
- package cn.lixyz.test.view;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import android.view.View;
- import android.widget.ImageButton;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import cn.lixyz.test.R;
- @SuppressLint("NewApi")
- public class MyCustomTitleBar extends RelativeLayout {
- // 定义自定义控件包含的组件
- private TextView title;
- private ImageButton button;
- // 定义控件的属性
- private String titleText;
- private float titleTextSize;
- private int titleTextColor;
- private Drawable titleButtonImage;
- // 为每个控件定义LayoutParams
- private LayoutParams textLayoutParams;
- private LayoutParams buttonLayoutParams;
- private customTitleBarListener listener;
- public interface customTitleBarListener {
- public void click();
- }
- public void setCustomTitleBarListener(customTitleBarListener listener) {
- this.listener = listener;
- }
- public MyCustomTitleBar(Context context, AttributeSet attrs) {
- super(context, attrs);
- // 获取我们定义的属性
- TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyCustomTitleBar);
- titleText = array.getString(R.styleable.MyCustomTitleBar_titleText);
- titleTextColor = array.getColor(R.styleable.MyCustomTitleBar_titleTextColor, 0);
- titleTextSize = array.getDimension(R.styleable.MyCustomTitleBar_titleTextSize, 10);
- titleButtonImage = array.getDrawable(R.styleable.MyCustomTitleBar_titleButtonImage);
- // 回收,以防出错
- array.recycle();
- // 新建包含的子组件
- title = new TextView(context);
- button = new ImageButton(context);
- // 为子组件赋值
- title.setText(titleText);
- title.setTextColor(titleTextColor);
- title.setTextSize(titleTextSize);
- button.setBackground(titleButtonImage);
- // 设置背景色
- setBackgroundColor(0xFF38373c);
- // 设置包含控件的属性并添加到view中
- textLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- textLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
- addView(title, textLayoutParams);
- buttonLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- buttonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
- addView(button, buttonLayoutParams);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listener.click();
- }
- });
- }
- }
attrs.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="MyCustomTitleBar">
- <attr name="titleText" format="string" />
- <attr name="titleTextSize" format="dimension" />
- <attr name="titleTextColor" format="color" />
- <attr name="titleButtonImage" format="reference" />
- </declare-styleable>
- </resources>
Android笔记(六十九) 仿微信界面(一)的更多相关文章
- Android笔记(六十八) Fragment总结
Fragment的产生: 为了适应各种尺寸的屏幕,谷歌推出Fragment,可以把Fragment成Activity的一个组成部分,它拥有自己的生命周期.可以接收并处理用户的各种事件,还可以动态的增删 ...
- Android笔记(十九) Android中的Fragment
通常我们使用Activity来展示界面,但是在手机上界面可能显示的很好看,但在平板上,因为平板的屏幕非常大,手机的界面放在平板上可能会出现控件被拉长.控件之间间距变大等问题.为了更好的体验效果,在Ac ...
- Android笔记(六十六) android中的动画——XML文件定义属性动画
除了直接在java代码中定义动画之外,还可以使用xml文件定义动画,以便重用. 如果想要使用XML来编写动画,首先要在res目录下面新建一个animator文件夹,所有属性动画的XML文件都应该存放在 ...
- Android笔记(六十五) android中的动画——属性动画(propertyanimation)
补间动画只能定义起始和结束两个帧在“透明度”.“旋转”.“倾斜”.“位移”4个方面的变化,逐帧动画也只能是播放多个图片,无法满足我们日常复杂的动画需求,所以谷歌在3.0开始,推出了属性动画(prope ...
- Android笔记(六十四) android中的动画——补间动画(tweened animation)
补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...
- Android笔记(六十二)网络框架volley
什么是Volley 很多时候,我们的APP都需要用到网络技术,使用HTTP协议来发送接收数据,谷歌推出了一个网络框架——volley,该框架适合进行数据量不大,但通信频繁的网络操作. 它的优点: (1 ...
- 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)
什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...
- Android ActionBar仿微信界面
ActionBar仿微信界面 1.学习了别人的两篇关于ActionBar博客,在结合别人的文章来仿造一下微信的界面: 思路如下:1).利用ActionBar生成界面的头部,在用ActionBar的Ac ...
- 转-Fragment+ViewPager组件(高仿微信界面)
http://www.cnblogs.com/lichenwei/p/3982302.html 什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开 ...
随机推荐
- 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 ...
- redis连接池——JedisPool和JedisCluster的介绍与使用
目录 Jedis使用方式的介绍 Redis连接池介绍 创建连接池配置文件 单机版的Redis连接池 集群版的Redis连接池 总结 Jedis使用方式的介绍 Jedis就是Java实现的操作Redis ...
- 2-5 【ngFor指令 事件的处理和样式绑定】顶部导航支持选中状态
索引的获取 first和last是布尔类型的 奇数偶数 []方括号表示的是数据绑定 ()圆括号就是事件绑定 . 开始代码 我们要实现的功能是菜单点击后,就变色 这样我们就得到了索引. 新建一个clas ...
- [译]如何比较master分支上与git上任意的一个老版本的区别?
原文来源:https://stackoverflow.com/questions/5586383/how-to-diff-one-file-to-an-arbitrary-version-in-git ...
- matlab学习笔记10_3关系运算符和逻辑运算符
一起来学matlab-matlab学习笔记10 10_3关系运算符和逻辑运算符 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用>张德丰 ...
- linux中安装robot环境
https://www.cnblogs.com/lgqboke/p/8252488.html(文中验证robotframework命令应该为 robot --version) 可能遇到的问题: 1.p ...
- 机器学习技法总结(一):支持向量机(linear support vector machine,dual support vector machine)
第一阶段技法: large margin (the relationship between large marin and regularization), hard-SVM,soft-SVM,du ...
- 在ensp上的进行的浮动路由
原理 实验模拟内容 搭建实验拓扑 相关参数 其他设置端口ip都一样,serial也一样(三个路由器都要设置的) 简单测试一下连通性 下面我们分别在路由器上配置所在网段的静态路由 配置完之后我们来查看一 ...
- Educational Codeforces Round 71
https://www.cnblogs.com/31415926535x/p/11460682.html 上午没课,做一套题,,练一下手感和思维,,教育场的71 ,,前两到没啥,,后面就做的磕磕巴巴的 ...
- Word样式教程
目录 写在前面 样式可以解决什么问题? 本文适合于 快速入门 一切皆样式 样式与格式的关系 如何修改样式 建立新的样式 样式的匹配和更新 根据样式更新所选段落 根据所选段落更新样式 小结 进一步了解 ...