亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android底部菜單欄實(shí)現(xiàn)原理與代碼

 更新時間:2013年01月02日 16:35:13   作者:  
底部菜單欄很重要,我看了一下很多應(yīng)用軟件都是用了底部菜單欄做,我這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數(shù)量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個人看起來還是不錯的,所以貼出來方便以后使用
上一個項(xiàng)目已經(jīng)做完了,這周基本上沒事,所以整理了下以前的項(xiàng)目,想把一些通用的部分封裝起來,這樣以后遇到相似的項(xiàng)目就不用重復(fù)發(fā)明輪子了,也節(jié)省了開發(fā)效率。今天把demo貼出來一是方便以后自己查詢,二是希望同時也能幫到大家。

底部菜單欄很重要,我看了一下很多應(yīng)用軟件都是用了底部菜單欄做。我這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數(shù)量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個人看起來還是不錯的,所以貼出來方便以后使用)。
先看一下做出來之后的效果:
 
以后使用的時候就可以換成自己項(xiàng)目的圖片和字體了,主框架不用變哈哈,
首先是要布局layout下xml文件 main.xml:
復(fù)制代碼 代碼如下:

<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bg_gray"
android:orientation="vertical" >
<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" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/main_tab_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/bottom1"
android:gravity="bottom"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/main_tab_addExam"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_0"
android:text="添加考試" />
<RadioButton
android:id="@+id/main_tab_myExam"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:checked="true"
android:drawableTop="@drawable/bg_checkbox_icon_menu_1"
android:text="我的考試" />
<RadioButton
android:id="@+id/main_tab_message"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_2"
android:text="我的通知" />
<RadioButton
android:id="@+id/main_tab_settings"
style="@style/MMTabButton"
android:layout_weight="1.0"
android:drawableTop="@drawable/bg_checkbox_icon_menu_3"
android:text="設(shè)置" />
</RadioGroup>
<TextView
android:id="@+id/main_tab_new_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginLeft="60dip"
android:layout_marginTop="1dip"
android:background="@drawable/tips"
android:gravity="center"
android:text="1"
android:textColor="#ffffff"
android:textSize="10sp"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
</TabHost>

在RadioGroup的外面加了一個FrameLayout,主要是為了使用TextView顯示消息數(shù)量,這里是居中靠左60dip,可能你會問直接寫死能支持多分辨率嗎,這個我在320*480的手機(jī)上試過沒問題的,因?yàn)閐ip是與設(shè)備無關(guān)的支持多分辨率,至于1280*800平板電腦這樣的分辨率我就不能保證了,哈哈!
接下來是樣式布局:
復(fù)制代碼 代碼如下:

<style name="MMTabButton">
<item name="android:textSize">12.0dip</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/bg_checkbox_menus</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:textColor">@color/white</item>
<item name="android:layout_weight">1.0</item>
<item name="android:paddingBottom">2.0dip</item>
<item name="android:paddingTop">2.0dip</item>
</style>

在drawable下bg_checkbox_menus.xml
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/mm_trans" />
<item
android:state_checked="true"
android:drawable="@drawable/home_btn_bg" />
</selector>

其他的那四個都合這個一樣點(diǎn)擊后圖片換成亮色的,所以就不一一貼出來了。
最后看MainActivity這個類:
復(fù)制代碼 代碼如下:

package cn.com.karl.test;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;
public class MainActivity extends TabActivity {
/** Called when the activity is first created. */
private TabHost tabHost;
private TextView main_tab_new_message;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);
main_tab_new_message.setVisibility(View.VISIBLE);
main_tab_new_message.setText("10");
tabHost=this.getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent=new Intent().setClass(this, AddExamActivity.class);
spec=tabHost.newTabSpec("添加考試").setIndicator("添加考試").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this,MyExamActivity.class);
spec=tabHost.newTabSpec("我的考試").setIndicator("我的考試").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, MyMessageActivity.class);
spec=tabHost.newTabSpec("我的通知").setIndicator("我的通知").setContent(intent);
tabHost.addTab(spec);
intent=new Intent().setClass(this, SettingActivity.class);
spec=tabHost.newTabSpec("設(shè)置").setIndicator("設(shè)置").setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.main_tab_addExam://添加考試
tabHost.setCurrentTabByTag("添加考試");
break;
case R.id.main_tab_myExam://我的考試
tabHost.setCurrentTabByTag("我的考試");
break;
case R.id.main_tab_message://我的通知
tabHost.setCurrentTabByTag("我的通知");
break;
case R.id.main_tab_settings://設(shè)置
tabHost.setCurrentTabByTag("設(shè)置");
break;
default:
//tabHost.setCurrentTabByTag("我的考試");
break;
}
}
});
}
}

這樣就完成了,主要還是使用了tabhost完成,tabhost有緩存機(jī)制這四個界面都會緩存到內(nèi)存中,這樣即有利也有弊,有利是因?yàn)榍袚Q的時候不用在重新加載了,有弊是因?yàn)榫彺嫠膫€界面會耗費(fèi)內(nèi)存較多一些。如果只想緩存一個界面以后下一篇我會使用ActivityGroup實(shí)現(xiàn)頂部滑動欄,就像網(wǎng)易新聞的頂部滑動欄我相信也是只緩存了一個界面,切換的時候是從數(shù)據(jù)庫加載的,所以第二次滑動加載會比較快。

相關(guān)文章

  • Android仿簡書搜索框效果的示例代碼

    Android仿簡書搜索框效果的示例代碼

    本篇文章主要介紹了Android仿簡書搜索框效果的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Android RecyclerView緩存復(fù)用原理解析

    Android RecyclerView緩存復(fù)用原理解析

    RecyclerView是Android一個更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動,也可以實(shí)現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法
    2022-11-11
  • Android實(shí)現(xiàn)手指觸控圖片縮放功能

    Android實(shí)現(xiàn)手指觸控圖片縮放功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手指觸控圖片縮放功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 總結(jié)安卓(Android)中常用的跳轉(zhuǎn)工具

    總結(jié)安卓(Android)中常用的跳轉(zhuǎn)工具

    在大家日常開發(fā)的時候經(jīng)常會用到各式各樣的跳轉(zhuǎn),如跳轉(zhuǎn)到QQ、微信聊天界面、跳轉(zhuǎn)到聯(lián)系人界面或者跳轉(zhuǎn)到瀏覽器和照相機(jī)等等之類的,本文將常用到的一些跳轉(zhuǎn)集合到一起,這樣更方便大家以后使用,有需要的小伙伴們可以參考借鑒。
    2016-08-08
  • Android 自定義View實(shí)現(xiàn)單擊和雙擊事件的方法

    Android 自定義View實(shí)現(xiàn)單擊和雙擊事件的方法

    下面小編就為大家?guī)硪黄狝ndroid 自定義View實(shí)現(xiàn)單擊和雙擊事件的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • Android開發(fā)實(shí)現(xiàn)應(yīng)用層面屏蔽狀態(tài)欄的方法小結(jié)

    Android開發(fā)實(shí)現(xiàn)應(yīng)用層面屏蔽狀態(tài)欄的方法小結(jié)

    這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)應(yīng)用層面屏蔽狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android屏蔽狀態(tài)欄的相關(guān)函數(shù)調(diào)用、權(quán)限控制及函數(shù)重寫等相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • android車牌識別系統(tǒng)EasyPR使用詳解

    android車牌識別系統(tǒng)EasyPR使用詳解

    這篇文章主要為大家詳細(xì)介紹了android車牌識別系統(tǒng)EasyPR使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android引用arr包的兩種方法

    Android引用arr包的兩種方法

    這篇文章介紹了android中引用arr包的兩種方法,jar包和arr包,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-11-11
  • Android開發(fā)手冊Chip監(jiān)聽及ChipGroup監(jiān)聽

    Android開發(fā)手冊Chip監(jiān)聽及ChipGroup監(jiān)聽

    這篇文章主要為大家介紹了Android開發(fā)手冊Chip監(jiān)聽及ChipGroup監(jiān)聽,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android 動態(tài)高斯模糊效果教程

    Android 動態(tài)高斯模糊效果教程

    本文主要介紹Android 動態(tài)高斯模糊效果教程,這里整理了詳細(xì)的資料及實(shí)例實(shí)現(xiàn)代碼,有興趣的小伙伴可以參考下
    2016-09-09

最新評論