android 底部固定菜单栏的实现(一)
来源:互联网 发布:运输算法 编辑:程序博客网 时间:2024/05/15 02:45
android底部固定按钮菜单栏在很多的APP的UI设计中都是必须重视的一个环节。而该开始这也是非常困扰我的一个模块。主要的考虑体现在以下几个部分:
1.如何让底部按钮都能沉在页面最下方,而不随着不同的布局而浮动。
2.如何让底部按钮和正常页面之间做配合,无缝衔接。
3.如何将所用的方法总结成为一个模板,可供重用。
在不断尝试的过程中,也陆陆续续遇到了一些问题。
比如原先我是设置了layout 的gravity 属性,后来发现当界面顶部的内容增多时,就会把下面菜单的内容给覆盖了。。
经过无数次的百度和google之后,我终于找到了解决的办法,那就是设置一个relativeLayout的布局,里面再设置两个线性布局,其中那个要在底部的布局就设置一个属性。 android:layout_alignParentBottom=”true”,然后这个布局及其里面的控件就会默认呆在底部了。。
下面我贴出自己的解决思路(简单的XML)
<?xml version="1.0" encoding="utf-8"?>// 最外面是一个相对的布局<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/ic_all_bg" android:orientation="vertical" ><LinearLayout //这是顶部的一个线性布局 android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/top" > <include layout="@layout/list"></include></LinearLayout> <LinearLayout //这个是要常呆在底部的布局 xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bottom" android:layout_alignParentBottom="true" //设置了这个属性之后,就会默认呆在屏幕的底部 > <Button android:id="@+id/addDiaryBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/addDiary" ></Button> </LinearLayout></RelativeLayout>
言归正传,还是来说说底部菜单栏的实现吧。
先介绍一种比较古老方式,在android4.0时已经被弃用。
1、使用TabActivity
这其中就有两种方式 ,一是通过TabWidget实现,另一种是隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏
实现方式一:通过TabWidget实现
这种方式主要是在布局中将TabWidget标签嵌套在RelativeLayout中,并且在TabWidget标签中中设置 android:layout_alignParentBottom=”true”
另外,下划线和选项卡之间的线去除的方法时在TabWidget标签中设置属性android:tabStripEnabled=”false”
//TabHost布局 <?xml version="1.0" encoding="UTF-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > //必须包含下列三个View <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <FrameLayout android:gravity="center" android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" /> //TabWidget位置在FrameLayout之下则显示在低部, 在之上则显示在顶部 <!-- tabStripEnabled属性去掉底部下划线与选项卡间的下划线 --> <!-- layout_alignParentBottom属性即可将其放在底部菜单栏,注意,必须在RelativeLayout里 --> <TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="0.0" android:tabStripEnabled="false" android:layout_alignParentBottom="true" /> </LinearLayout> </TabHost>
Activity类:
public class TabTest extends TabActivity { private TabWidget mTabWidget; private TabHost mTabHost; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_tabs); mTabHost = getTabHost(); //将要显示的Activity载入TabHost控件 //要显示的Activity由自己自由创建 setTabIndicator("one", 1, new Intent(this, OneActivity.class)); setTabIndicator("Two", 2, new Intent(this, TwoActivity.class)); setTabIndicator("Three", 3, new Intent(this, OneActivity.class)); setTabIndicator("Four", 4, new Intent(this, TwoActivity.class)); } private void setTabIndicator(String title, int nId, Intent intent) { //使用指定Tab样式 View view = LayoutInflater.from(this.mTabHost.getContext()) .inflate(R.layout.tab_style, null); TextView text = (TextView)view.findViewById(R.id.tab_label); String strId = String.valueOf(nId); text.setText(title); //创建一个新Tab TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(strId) .setIndicator(view).setContent(intent); //加载新Tab mTabHost.addTab(localTabSpec); } }
实现方式二:隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏
这种方式更漂亮,网上基本用的是这种方式,通过setCurrentTabByTag来切换不同的选项卡
main.xml<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0"/> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="gone"/> <RadioGroup android:id="@+id/main_tab" android:background="@drawable/maintab_toolbar_bg" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_gravity="bottom"> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_home" android:drawableTop="@drawable/icon_1_n" android:id="@+id/radio_button0" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" android:id="@+id/radio_button1" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" android:id="@+id/radio_button2" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" android:id="@+id/radio_button3" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" android:id="@+id/radio_button4" style="@style/main_tab_bottom"/> </RadioGroup> </LinearLayout></TabHost>
drawable/home_btn_bg.xml:切换时的效果
<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" /> <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" /> <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" /> <item android:drawable="@drawable/transparent" /></selector>
string/dimens.xml 尺寸文件
<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="bottom_tab_padding_drawable">2.0dip</dimen> <dimen name="bottom_tab_padding_up">5.0dip</dimen> <dimen name="bottom_tab_font_size">10.0dip</dimen></resources>
string/drawables.xml 设置为透明
<?xml version="1.0" encoding="utf-8"?><resources><item type="drawable" name="transparent">#00000000</item></resources>
string/styles.xml 样式文件
<?xml version="1.0" encoding="utf-8"?><resources><style name="main_tab_bottom"> <item name="android:textSize">@dimen/bottom_tab_font_size</item> <item name="android:textColor">#ffffffff</item> <item name="android:ellipsize">marquee</item> <item name="android:gravity">center_horizontal</item> <item name="android:background">@drawable/home_btn_bg</item> <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:button">@null</item> <item name="android:singleLine">true</item> <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item> <item name="android:layout_weight">1.0</item></style></resources>
Activity类:
public class MainTabActivity extends TabActivity implements OnCheckedChangeListener{ private RadioGroup mainTab; private TabHost tabhost; private Intent iHome; private Intent iNews; private Intent iInfo; private Intent iSearch; private Intent iMore; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); mainTab=(RadioGroup)findViewById(R.id.main_tab); mainTab.setOnCheckedChangeListener(this); tabhost = getTabHost(); iHome = new Intent(this, HomeActivity.class); tabhost.addTab(tabhost.newTabSpec("iHome") .setIndicator(getResources().getString(R.string.main_home), getResources().getDrawable(R.drawable.icon_1_n)) .setContent(iHome)); iNews = new Intent(this, NewsActivity.class); tabhost.addTab(tabhost.newTabSpec("iNews") .setIndicator(getResources().getString(R.string.main_news), getResources().getDrawable(R.drawable.icon_2_n)) .setContent(iNews)); iInfo = new Intent(this, MyInfoActivity.class); tabhost.addTab(tabhost.newTabSpec("iInfo") .setIndicator(getResources().getString(R.string.main_my_info), getResources().getDrawable(R.drawable.icon_3_n)) .setContent(iInfo)); iSearch = new Intent(this,SearchActivity.class); tabhost.addTab(tabhost.newTabSpec("iSearch") .setIndicator(getResources().getString(R.string.menu_search), getResources().getDrawable(R.drawable.icon_4_n)) .setContent(iSearch)); //添加Tab iMore = new Intent(this, MoreActivity.class); tabhost.addTab(tabhost.newTabSpec("iMore") .setIndicator(getResources().getString(R.string.more), getResources().getDrawable(R.drawable.icon_5_n)) .setContent(iMore)); } @Override /** * 选项卡切换 */ public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radio_button0: this.tabhost.setCurrentTabByTag("iHome"); break; case R.id.radio_button1: this.tabhost.setCurrentTabByTag("iNews"); break; case R.id.radio_button2: this.tabhost.setCurrentTabByTag("iInfo"); break; case R.id.radio_button3: this.tabhost.setCurrentTabByTag("iSearch"); break; case R.id.radio_button4: this.tabhost.setCurrentTabByTag("iMore"); break; } }}
TabActivity因为其自身的一些缺陷,已经被替换成Fragment,所以本部分就简单介绍到这个地方。接下来将会介绍Fragment
- android 底部固定菜单栏的实现(一)
- android实现底部菜单栏
- android实现底部菜单栏
- android底部菜单栏实现
- Android--底部菜单栏实现
- Android 底部菜单栏实现
- android实现底部菜单栏
- ViewPager+RadioGroup实现底部菜单栏(一)
- 底部菜单栏(一) TabHost实现
- (4.1.19)【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- 【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏
- ThreadHandle And CloseHandel
- cocos2dx-粒子特效
- 循环小数(Repeating Decimals)
- Manacher-求最长回文字符串
- logstash入门(简单而全面)
- android 底部固定菜单栏的实现(一)
- HUD 3911 Black And White 线段树 区间更新 + 区间合并
- [贪心]poj3045 Cow Acrobats
- Linux 前后台程序
- 如何调试Javascript代码
- Java基础-IO-其它流(笔记)
- 从服务器读取一张图片
- 南邮 OJ 2069 赵信的往事
- 九度oj 1451