Fragment实现微信Tab界面(不可通过界面左右拖动切换界面,只可以由按钮切换)
微信 实现 通过 可以 界面 切换 按钮 不可
2023-09-14 09:00:25 时间
一.主要步骤:
1.创建工程项目,建立四个Fragment.java文件(如 weixin_Fragment.java)
2.建立四个tab.xml文件(如:tab01.xml)
3.创建top.xml文件(用于显示界面标题),bottom.xml文件(用于显示界面底下的四个ImageButton及对应textView)
4.完成activity_main.xml布局 (中间部分用FrameLayout)
5.对需要的亮和暗色图片放入drawable中
二.代码实现:
1.Fragment.java文件格式:
如:
public class Weixin_Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab01,container,false);
}
2.tab.xml 代码格式:
如:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
TextView
android:text="This is Weixin Tab"
android:textSize="30sp"
android:gravity="center"
android:text
android:layout_width="match_parent"
android:layout_height="match_parent" /
/LinearLayout
(这里只是作为识别界面用所以比较简单,平常用可以进行扩展)
3.标题栏 top.xml格式:
如:
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#464646"
android:gravity="center"
android:layout_height="55dp"
TextView
android:gravity="center"
android:text="微信"
android:textSize="20sp"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent" /
/LinearLayout
4.底部按钮布局:bottom.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#ffffff"
android:layout_height="65dp"
LinearLayout
android:id="@+id/tab_weixin"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
ImageButton
android:clickable="false"
android:id="@+id/tab_weixin_img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/weixin_anse"
android:background="#0000"
/
TextView
android:text="微信"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /
/LinearLayout
LinearLayout
android:id="@+id/tab_friend"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
!--当点击图片按钮没有作用时可设置: android:clickable="false" 图片不可点击即让LinearLayout 去实现点击事件--
ImageButton
android:clickable="false"
android:id="@+id/tab_friend_img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/friend_anse"
android:background="#00000000"
/
!--android:background="#00000000" 设置为透明 和四个0效果一样--
TextView
android:text="朋友"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /
/LinearLayout
LinearLayout
android:id="@+id/tab_address"
android:layout_width="0dp"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
android:layout_height="match_parent"
ImageButton
android:clickable="false"
android:id="@+id/tab_address_img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/zhibo_anse"
android:background="#00000000" //八个0和四个0均表示透明
/
TextView
android:text="直播"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /
/LinearLayout
LinearLayout
android:id="@+id/tab_setting"
android:layout_width="0dp" //宽度方向平分空间最好设为0dp
android:orientation="vertical"
android:layout_weight="1" //分享剩余空间
android:gravity="center"
android:layout_height="match_parent"
ImageButton
android:clickable="false"
android:id="@+id/tab_setting_img"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/shezhi_anse"
android:background="#00000000"
/
TextView
android:text="设置"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp" /
/LinearLayout
/LinearLayout
5.对主界面布局:
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
include layout="@layout/top"/ //引入布局
!--与ViewPager实现Tab不同的是这里不用ViewPager--
FrameLayout
android:id="@+id/id_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/FrameLayout
include layout="@layout/bottom"/ //引入布局
6.MianActivity.java
//使用 v4 包下的 Fragment事务 需要改为继承FragmentActivity
public class MainActivity extends FragmentActivity implements View.OnClickListener{
private LinearLayout mTabWeixin;
private LinearLayout mTabFriend;
private LinearLayout mTabAddress;
private LinearLayout mTabSetting;
private ImageButton mImgWeixin;
private ImageButton mImgFrd;
private ImageButton mImgaddress;
private ImageButton mImgSetting;
private Fragment weixinFragnment; //import android.support.v4.app.Fragment; 使用的包一定要一致
private Fragment friendFragnment;
private Fragment addressFragnment;
private Fragment settingFragnment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题
setContentView(R.layout.activity_main);
initView();//初始化控件
initEvent();//初始化事件
setSelect(0);//对事物方法调用显示第一个界面
}
private void initEvent() {
//先给每一个layout设置点击监听 是LinearLayout
mTabWeixin.setOnClickListener(this);
mTabFriend.setOnClickListener(this);
mTabAddress.setOnClickListener(this);
mTabSetting.setOnClickListener(this);
}
private void initView() {
mTabWeixin= (LinearLayout) findViewById(R.id.tab_weixin);
mTabFriend= (LinearLayout) findViewById(R.id.tab_friend);
mTabAddress= (LinearLayout) findViewById(R.id.tab_address);
mTabSetting= (LinearLayout) findViewById(R.id.tab_setting);
mImgWeixin= (ImageButton) findViewById( R.id.tab_weixin_img);
mImgFrd= (ImageButton) findViewById(R.id.tab_friend_img);
mImgaddress= (ImageButton) findViewById(R.id.tab_address_img);
mImgSetting= (ImageButton) findViewById(R.id.tab_setting_img);
}
//自定义一个方法
private void setSelect(int i){
// FragmentManager fm = getFragmentManager(); //先拿到管理器
FragmentManager fm = getSupportFragmentManager(); //使用V4包下的Fragment是的事务管理器
FragmentTransaction transaction = fm.beginTransaction(); //开启一个事务transaction
hideFragment(transaction); //自定义一个函数先对所有事务进行隐藏
//将图片切换为亮色
//切换界面
switch (i){ //切换图片为亮色
case 0:
{
if(weixinFragnment==null){
//为空则初始化他
weixinFragnment=new Weixin_Fragment();
transaction.add(R.id.id_content,weixinFragnment); //初始化Fragment
}else{
//否则对其进行显示
transaction.show(weixinFragnment);
}
mImgWeixin.setImageResource(R.drawable.weixin_lang); //切换图片
break;
}
case 1:
{
if(friendFragnment==null){
//为空则初始化他
friendFragnment=new Friend_Fragment();
transaction.add(R.id.id_content,friendFragnment); //初始化Fragment
}else{
//否则对其进行显示
transaction.show(friendFragnment);
}
mImgFrd.setImageResource(R.drawable.friend_liang);
break;
}
case 2:
{
if(addressFragnment==null){
//为空则初始化他
addressFragnment=new Address_Fragment();
transaction.add(R.id.id_content,addressFragnment); //初始化Fragment
}else{
//否则对其进行显示
transaction.show(addressFragnment);
}
mImgaddress.setImageResource(R.drawable.zhibo_liang);
break;
}
case 3:
{
if(settingFragnment==null){
//为空则初始化他
settingFragnment=new Setting_Fragment();
transaction.add(R.id.id_content,settingFragnment); //初始化Fragment
}else{
//否则对其进行显示
transaction.show(settingFragnment);
}
mImgSetting.setImageResource(R.drawable.shezhi_liang);
break;
}
}//switch
transaction.commit();//提交事务
}//setSelect()
//隐藏事务方法
private void hideFragment(FragmentTransaction transaction) { //对不为空的Fragment隐藏
if(weixinFragnment!=null){
transaction.hide(weixinFragnment);
}
if(friendFragnment!=null){
transaction.hide(friendFragnment);
}
if(addressFragnment!=null){
transaction.hide(addressFragnment);
}
if(settingFragnment!=null){
transaction.hide(settingFragnment);
}
} //hideFragment()
@Override
public void onClick(View view) {
resetImg();//设置暗色
switch (view.getId()){
case R.id.tab_weixin:
{
setSelect(0);
mImgWeixin.setImageResource(R.drawable.weixin_lang); //将点击的图标设置为亮色
break;
}
case R.id.tab_friend:
{
setSelect(1);
mImgFrd.setImageResource(R.drawable.friend_liang);
break;
}
case R.id.tab_address:
{
setSelect(2);
mImgaddress.setImageResource(R.drawable.zhibo_liang);
break;
}
case R.id.tab_setting:
{
setSelect(3);
mImgSetting.setImageResource(R.drawable.shezhi_liang);
break;
}
}
}//onClick
//图片设置为暗色
private void resetImg() {
mImgWeixin.setImageResource(R.drawable.weixin_anse);
mImgFrd.setImageResource(R.drawable.friend_anse);
mImgaddress.setImageResource(R.drawable.zhibo_anse);
mImgSetting.setImageResource(R.drawable.shezhi_anse);
}
}
上
应该有很多Android的用户熟悉上面这图。 (本文同步发布于:http://www.52im.net/thread-1099-1-1.html) 2、事件背景 国庆前几天,微信Android大量用户反馈接收或发送类似“15。
同一个界面内取微信的OPENID和调用微信的分享接口 步骤如下,1:判断URL是否有CODE参数传入,没有则拼接那个微信跳转连接,然后redirect2:有CODE传入,调用微信接口,根据code获取openid和access_token,注意这一步取到的token是没用的,这里已经取到openid了,下面开始取微信JS SDK的接口注入配置3:调用微...
微信点餐系统的开发与实现 随着互联网技术逐渐的深入到生活,人们的生活消费习惯,已经发生很大的变化。就餐厅用餐而言,互联网技术和移动互联网技术的应用也己相关普及,比如早几年前就出现的餐厅点餐系统,就通过信息化的技术手段代替原来纸质菜单点餐的传统方式。这种方式一是可以方便顾客实现点餐叫号,二是方便商家进行人单合一的统一管理,减少了报单出错率,提升了用户的体验,所以得以大面积的应用和普及。 而移动互联网的出现,让智能手机的赋能更大广泛,腾讯微信适时推出微信小程序,使得智能手机的用户可以通过微信进行相应的信息化管理和参与,比如目前大面积应用的小程序商城,就将商业的业态从传统的PC互联网直接植入手机移动互
微信Java开发工具包的使用 之前我在《Java调用微信登录以及eclipse远程调试》一文中说到了Java程序怎么调用微信登录,不过那篇文章中使用的是手工方式,本文将介绍如何使用第三方SDK(binarywang)进行微信网页授权以及获取微信用户信息。
相关文章
- [分享] 微信支付中商户对个人用户付款案例
- 微信小程序wx.request请求服务器json数据并渲染到页面
- 微信小程序_(map)简单的小地图
- PHP实现微信提现功能
- 微信第三方登陆,无需注册一键登录,获取用户信息,PHP实现方法.
- 实现RTSP网站微信直播方案EasyNVR(linux版)部署问题之:ERR_CONTENT_LENGTH_MISMATCH
- 一种流量成本节省60%以上的手机直播微信直播H5直播幼儿园直播方案
- 通过摄像机视频设备或者流媒体服务器SDK获取到数据转换成RTMP流实现网页/手机微信播放
- 通过微信实现照片的拼接
- 微信config:invalid signature
- 【微信小程序】---- redux 在原生微信小程序的使用实例
- 微信小程序----简易双向绑定
- 微信小程序----Grid(九宫格)(flex实现九宫格布局)
- 微信小程序----map组件实现(获取定位城市天气或者指定城市天气数据)
- 微信小程序----progress组件
- SAE上微信公众账号开发参考
- 微信小程序开发系列 (二) :微信小程序的单步调试和控制器实现步骤概述
- SAP Field Service Management 和微信集成的案例分享和实现介绍
- 微信小程序开发系列 (二) :微信小程序的单步调试和控制器实现步骤概述
- 移动端h5网页、微信网页调试之利用vConsole真机调试+显示控制台打印信息、调试接口(附带vue项目里的具体使用方法)
- Python微信公众号教程基础篇——收发文本消息
- 企业微信机器人
- webstorm开发微信小程序
- 未来十年营销的主流趋势:微信营销
- 微信营销之《序言》
- Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现
- centos 7 zabbix搭建以及邮件微信告警实现
- Linux|centos7下部署安装alertmanager并实现邮箱和微信告警(基础篇---三)
- WordPress对接微信小程序遇到的问题
- Zabbix 实现微信报警