Android 仿微信底部菜单

Stella981
• 阅读 819

今天终于把公司的界面原型做完了,哈哈,提前完成正好趁现在有时间更新下最近学到的一些特殊效果。其中这个仿微信的底部菜单,真的要感谢家辉兄弟,我才得以解决。首先看一下实现后效果。

    就下面的那个底部栏,下面看一下实现代码吧!

    首先是布局main.xml:

查看源码

打印 ?

01

  

02

<TabHostandroid:id="@android :id/tabhost"android:layout_width="fill_parent"android:layout_height="fill_parent"  

03

  xmlns:android="http://schemas.android.com/apk/res/android">  

04

    LinearLayoutandroid:orientation="vertical"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

05

        <FrameLayoutandroid:id="@android :id/tabcontent"android:layout_width="fill_parent"android:layout_height="0.0dip"android:layout_weight="1.0"/>  

06

        <TabWidgetandroid:id="@android :id/tabs"android:visibility="gone"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="0.0"/>  

07

    

08

        LinearLayoutandroid:gravity="bottom"android:layout\_gravity="bottom"android:orientation="horizontal"android:id="@+id/main\_tab\_group"android:background="@drawable/bottom1"android:paddingTop="2.0dip"android:layout\_width="fill\_parent"android:layout\_height="wrap\_content"  

09

            FrameLayoutandroid:background="@null"android:layout\_width="0.0dip"android:layout\_height="fill\_parent"android:layout\_weight="1.0"  

10

                LinearLayoutandroid:id="@+id/main\_layout\_addExam"android:gravity="bottom|center"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

11

                    RadioButtonandroid:id="@+id/main\_tab\_addExam"android:checked="true"android:text="添加考试"android:drawableTop="@drawable/label\_1"style="@style/MMTabButton"/  

12

                  

13

                <LinearLayout android:gravity="top|right|center"android:paddingRight="10.0dip"android:layout_width="fill_parent"android:layout_height="fill_parent">  

14

                    <TextViewandroid:textSize="10.0dip"android:text="1"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main_tab_unread_tv"android:background="@drawable/new_icon_2"android:visibility="invisible"android:layout_width="wrap_content"android:layout_height="wrap_content" />  

15

                  

16

              

17

             FrameLayoutandroid:background="@null"android:layout\_width="0.0dip"android:layout\_height="fill\_parent"android:layout\_weight="1.0"  

18

                LinearLayoutandroid:id="@+id/main\_layout\_myExam"android:gravity="bottom|center"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

19

                     RadioButtonandroid:id="@+id/main\_tab\_myExam"android:text="我的考试"android:drawableTop="@drawable/label\_2"style="@style/MMTabButton"/  

20

                  

21

                LinearLayoutandroid:gravity="top|right|center"android:paddingRight="10.0dip"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

22

                    TextViewandroid:textSize="10.0dip"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main\_tab\_address"android:background="@drawable/new\_icon\_2"android:visibility="invisible"android:layout\_width="wrap\_content"android:layout\_height="wrap\_content"android:text="1"/  

23

                  

24

              

25

            FrameLayoutandroid:background="@null"android:layout\_width="0.0dip"android:layout\_height="fill\_parent"android:layout\_weight="1.0"  

26

                LinearLayoutandroid:id="@+id/main\_layout\_message"android:gravity="bottom|center"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

27

                    RadioButtonandroid:id="@+id/main\_tab\_message"android:text="考试资讯"android:drawableTop="@drawable/label\_3"style="@style/MMTabButton"/  

28

                  

29

                LinearLayoutandroid:gravity="top|right|center"android:paddingRight="10.0dip"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

30

                    TextViewandroid:textSize="12.0dip"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main\_tab\_new\_tv"android:background="@drawable/new\_icon\_2"android:visibility="visible"android:layout\_width="wrap\_content"android:layout\_height="wrap\_content"android:text="1"/  

31

                  

32

              

33

            FrameLayoutandroid:background="@null"android:layout\_width="0.0dip"android:layout\_height="fill\_parent"android:layout\_weight="1.0"  

34

                LinearLayoutandroid:id="@+id/main\_layout\_settings"android:gravity="bottom|center"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

35

                    RadioButtonandroid:id="@+id/main\_tab\_settings"android:text="设置"android:drawableTop="@drawable/label4"style="@style/MMTabButton"/  

36

                  

37

                LinearLayoutandroid:gravity="top|right|center"android:paddingRight="10.0dip"android:layout\_width="fill\_parent"android:layout\_height="fill\_parent"  

38

                    TextViewandroid:textSize="10.0dip"android:textColor="#ffffff"android:gravity="center"android:id="@+id/main\_tab\_setting\_new\_tv"android:background="@drawable/new\_icon\_2"android:paddingLeft="6.0dip"android:paddingRight="6.0dip"android:visibility="invisible"android:layout\_width="wrap\_content"android:layout\_height="wrap\_content"android:text="1"/  

39

                  

40

              

41

          

42

      

43

      其实就是用FrameLayout,微信也是这样布局的我反编译过。这样就可以灵活的布局那个红色的图标了。

      下面看一下activity切换代码:

查看源码

打印 ?

001

importandroid.app.TabActivity;

002

importandroid.content.Intent;

003

importandroid.os.Bundle;

004

importandroid.view.View;

005

importandroid.view.View.OnClickListener;

006

importandroid.view.Window;

007

importandroid.widget.LinearLayout;

008

importandroid.widget.RadioButton;

009

importandroid.widget.TabHost;

010

importandroid.widget.TextView;

011

  

012

publicclassMainTabActivityextendsTabActivity {

013

  

014

    TabHost tabHost;

015

    privateTextView main_tab_unread_tv;

016

    privateRadioButton main_tab_addExam, main_tab_myExam,main_tab_message,main_tab_settings;

017

    privateLinearLayout main_layout_addExam,main_layout_myExam,main_layout_message,main_layout_settings;

018

  

019

    publicvoidonCreate(Bundle savedInstanceState) {

020

        super.onCreate(savedInstanceState);

021

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

022

        setContentView(R.layout.main);

023

  

024

        initTab();

025

          

026

        init();

027

          

028

    }

029

  

030

    privatevoidinit() {

031

  

032

        main_tab_addExam = (RadioButton) findViewById(R.id.main_tab_addExam);

033

  

034

        main_tab_myExam = (RadioButton) findViewById(R.id.main_tab_myExam);

035

          

036

        main_tab_message=(RadioButton) findViewById(R.id.main_tab_message);

037

          

038

        main_tab_settings=(RadioButton) findViewById(R.id.main_tab_settings);

039

          

040

        main_layout_addExam=(LinearLayout) findViewById(R.id.main_layout_addExam);

041

        main_layout_myExam=(LinearLayout) findViewById(R.id.main_layout_myExam);

042

        main_layout_message=(LinearLayout) findViewById(R.id.main_layout_message);

043

        main_layout_settings=(LinearLayout) findViewById(R.id.main_layout_settings);

044

          

045

        main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_one_one),null,null);

046

        main_layout_addExam.setBackgroundResource(R.drawable.labelbg);

047

          

048

        main_tab_unread_tv=(TextView) findViewById(R.id.main_tab_unread_tv);

049

        //main_tab_unread_tv.setVisibility(View.VISIBLE);

050

        //main_tab_unread_tv.setText("3");

051

          

052

        main_tab_addExam.setOnClickListener(newOnClickListener() {

053

              

054

            publicvoidonClick(View arg0) {

055

                tabHost.setCurrentTabByTag("first");

056

                /*main_tab_addExam.setBackgroundResource(R.drawable.label_one_one);

057

                main_tab_myExam.setBackgroundResource(R.drawable.label_2);

058

                main_tab_message.setBackgroundResource(R.drawable.label_3);*/

059

                //main_tab_addExam.setCompoundDrawables(null, getResources().getDrawable(R.drawable.label_one_one), null, null);

060

                main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_one_one), null, null);

061

                main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_2), null, null);

062

                main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_3), null, null);

063

                main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label4), null, null);

064

                main_layout_addExam.setBackgroundResource(R.drawable.labelbg);

065

                main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);

066

                main_layout_message.setBackgroundResource(R.drawable.mm_trans);

067

                main_layout_settings.setBackgroundResource(R.drawable.mm_trans);

068

            }

069

        });

070

          

071

        main_tab_myExam.setOnClickListener(new OnClickListener() {

072

              

073

            public void onClick(View arg0) {

074

                tabHost.setCurrentTabByTag("second");

075

                /*main_tab_addExam.setBackgroundResource(R.drawable.label_1);

076

                main_tab_myExam.setBackgroundResource(R.drawable.label_two_one);

077

                main_tab_message.setBackgroundResource(R.drawable.label_3);*/

078

                main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_1), null, null);

079

                main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_two_one), null, null);

080

                main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_3), null, null);

081

                main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label4), null, null);

082

                main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);

083

                main_layout_myExam.setBackgroundResource(R.drawable.labelbg);

084

                main_layout_message.setBackgroundResource(R.drawable.mm_trans);

085

                main_layout_settings.setBackgroundResource(R.drawable.mm_trans);

086

            }

087

        });

088

        main_tab_message.setOnClickListener(new OnClickListener() {

089

              

090

            public void onClick(View v) {

091

                // TODO Auto-generated method stub

092

                tabHost.setCurrentTabByTag("third");

093

                /*main_tab_addExam.setBackgroundResource(R.drawable.label_1);

094

                main_tab_myExam.setBackgroundResource(R.drawable.label_2);

095

                main_tab_message.setBackgroundResource(R.drawable.label_three_one);*/

096

                main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_1),null,null);

097

                main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_2),null,null);

098

                main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_three_one),null,null);

099

                main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label4),null,null);

100

                main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);

101

                main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);

102

                main_layout_message.setBackgroundResource(R.drawable.labelbg);

103

                main_layout_settings.setBackgroundResource(R.drawable.mm_trans);

104

            }

105

        });

106

          

107

        main_tab_settings.setOnClickListener(newOnClickListener() {

108

              

109

            publicvoidonClick(View v) {

110

                // TODO Auto-generated method stub

111

                tabHost.setCurrentTabByTag("four");

112

                main_tab_addExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_1),null,null);

113

                main_tab_myExam.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_2),null,null);

114

                main_tab_message.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_3),null,null);

115

                main_tab_settings.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.label_four_one),null,null);

116

                main_layout_addExam.setBackgroundResource(R.drawable.mm_trans);

117

                main_layout_myExam.setBackgroundResource(R.drawable.mm_trans);

118

                main_layout_message.setBackgroundResource(R.drawable.mm_trans);

119

                main_layout_settings.setBackgroundResource(R.drawable.labelbg);

120

            }

121

        });

122

    }

123

      

124

      

125

    privatevoidinitTab(){

126

        tabHost=getTabHost();

127

        tabHost.addTab(tabHost.newTabSpec("first").setIndicator("first").setContent(

128

                newIntent(this, AddExamActivity.class)));

129

  

130

        tabHost.addTab(tabHost.newTabSpec("second").setIndicator("second").setContent(

131

                newIntent(this, MyExamActivity.class)));

132

          

133

        tabHost.addTab(tabHost.newTabSpec("third").setIndicator("third").setContent(

134

                newIntent(this, MessageActivity.class)));

135

          

136

        tabHost.addTab(tabHost.newTabSpec("four").setIndicator("four").setContent(

137

                newIntent(this, SettingActivity.class)));

138

  

139

    }

140

  

141

}

        setCompoundDrawablesWithIntrinsicBounds方法是我用来设置顶部图片的,用来替换 android:drawableTop这个属性的背景图,至于为什么那么麻烦每次都要设置背景图片颜色,是因为没用用RadioGroup包含 RadioButton,所以RadioButton就不能做到单个切换。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Easter79 Easter79
2年前
thinkcmf+jsapi 实现微信支付
首先从小程序端接收订单号、金额等参数,然后后台进行统一下单,把微信支付的订单号返回,在把订单号发送给前台,前台拉起支付,返回参数后更改支付状态。。。回调publicfunctionnotify(){$wechatDb::name('wechat')where('status',1)find();
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这