Android 仿微信底部菜单

2019-03-02 00:06|来源: 网路

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

    

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

    首先是布局main.xml:

01 <?xmlversion="1.0"encoding="UTF-8"?>  
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                 </LinearLayout>  
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                 </LinearLayout>  
16             </FrameLayout>  
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                 </LinearLayout>  
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                 </LinearLayout>  
24             </FrameLayout>  
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                 </LinearLayout>  
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                 </LinearLayout>  
32             </FrameLayout>  
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                 </LinearLayout>  
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                 </LinearLayout>  
40             </FrameLayout>  
41         </LinearLayout>  
42     </LinearLayout>  
43 </TabHost>
      其实就是用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就不能做到单个切换。
转自:http://my.oschina.net/u/870990/blog/142754

相关问答

更多
  • 微信2下载[2024-02-01]

    您好,打开手机浏览器或者是应用商城,然后输入微信进行搜索,在搜索结果中下载安装这款软件即可
  • webView为什么要重新排版呢,访问的什么页面显示的就是什么页面啊。通过webView。getSettings的属性这是webView支持JS。Form表单,数据库,缓存。缩放。并且RequestFoucs。通过这些属性的设置你想要得页面基本都能满足了。特例的JS等需要重新写一下。估计你的问题是不是你没有获得焦点,通过webView。RequestFoucs应该可以了吧就。我最近也一直在做WebView,一些基本的都没有什么问题的。有什么问题再问我吧。大家互相学习。
  • 用你上网的IP地址
  • ①先把第三方Recovery解压好,把手机关机,按住电源键+音量下进入FastBoot模式,根据提示刷入Recovery,英版开发版Root权限完整,不用再刷SuperSu卡刷包,国内开发板未知,MIUI7稳定版需要刷入SuperSu卡刷包获取Root权限。 ②刷入xposed-v75-sdk22-arm64-MIUI-edition-by-SolarWarez-20151015卡刷包,V75和V78均可刷入,刷写完成后安装Xposed installer即可。 ③安装模块:以应用变量为例,安装好软件之后, ...
  • 菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu),以下说的是创建OptionsMenu   一、概述 public boolean onCreateOptionsMenu(Menu menu):使用此方法调用OptionsMenu 。 public boolean onOptionsItemSelected(MenuItem item):选中菜单项后发生的动作。 publ ...
  • 使用Java反射机制 IActivityManager与ActivityManagerNative都是非公开类,使用Java反射去调用其中的方法。 但是这个弊端是显而易见的,上述两种方法都是去更改系统的语言的类型,功能和你去设置页面去设置语言类型的效果一样。发现对当前系统设置了新的Locale后,不单自己的应用语系改变了,系统所有的应用语系都改变了。这肯定是不合理的。或许你会说,在退出app的时候,大不了再把语言给改回来呗。可是在Android里面,杀死APP的原因多种多样,有内存杀,有用户强杀,有滑动侧滑 ...
  • 晕,你还以为你电脑多强啊,用windows模拟安卓,i7的cpu模拟起来都卡,想玩安卓买个机子不就行咯,windows上的安卓是给开发人员测试用的,不能日常使用
  • 小程序本身是微信提供的一个平台,使用和注册都是不需要钱的(企业需要300认证费)。注册下来的名字也永久是你的,就是小程序你要用起来里面的功能是需要开发的,要么自己开发要么找第三方合作。
  • 看到您的目标是SDK 17,您可以使用SplitBar,它是默认ActionBar上的一个选项: http : //developer.android.com/guide/topics/ui/actionbar.html#SplitBar 。 您需要在清单文件中需要的每个活动中声明它,例如:
  • 无法控制选项菜单中特定项目的大小。 但是,您可以尝试更改它们的顺序。 使用3个项目时,第一个项目将跨越整个顶行,最后两个项目将分割在底行。 如果订单非常重要,请在屏幕底部创建一个linearlayout,其中包含您想要的尺寸和顺序的按钮。 然后覆盖onCreateOptionsMenu函数并切换包含菜单的linearlayout的Visible属性。 There is no way to control the size of specific items in the options menu. You ...