麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

android底部菜單欄實現原理與代碼

2020-04-11 12:37:17
字體:
來源:轉載
供稿:網友
上一個項目已經做完了,這周基本上沒事,所以整理了下以前的項目,想把一些通用的部分封裝起來,這樣以后遇到相似的項目就不用重復發明輪子了,也節省了開發效率。今天把demo貼出來一是方便以后自己查詢,二是希望同時也能幫到大家。

底部菜單欄很重要,我看了一下很多應用軟件都是用了底部菜單欄做。我這里使用了tabhost做了一種通用的(就是可以像微信那樣顯示未讀消息數量的,雖然之前也做過但是layout下的xml寫的太臃腫,這里去掉了很多不必要的層,個人看起來還是不錯的,所以貼出來方便以后使用)。
先看一下做出來之后的效果:
 
以后使用的時候就可以換成自己項目的圖片和字體了,主框架不用變哈哈,
首先是要布局layout下xml文件 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: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="設置" />
</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顯示消息數量,這里是居中靠左60dip,可能你會問直接寫死能支持多分辨率嗎,這個我在320*480的手機上試過沒問題的,因為dip是與設備無關的支持多分辨率,至于1280*800平板電腦這樣的分辨率我就不能保證了,哈哈!
接下來是樣式布局:
復制代碼 代碼如下:

<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
復制代碼 代碼如下:

<?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>

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

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("設置").setIndicator("設置").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://設置
tabHost.setCurrentTabByTag("設置");
break;
default:
//tabHost.setCurrentTabByTag("我的考試");
break;
}
}
});
}
}

這樣就完成了,主要還是使用了tabhost完成,tabhost有緩存機制這四個界面都會緩存到內存中,這樣即有利也有弊,有利是因為切換的時候不用在重新加載了,有弊是因為緩存四個界面會耗費內存較多一些。如果只想緩存一個界面以后下一篇我會使用ActivityGroup實現頂部滑動欄,就像網易新聞的頂部滑動欄我相信也是只緩存了一個界面,切換的時候是從數據庫加載的,所以第二次滑動加載會比較快。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嗯~啊~用力~高h | 激情小说另类 | 欧美视频在线观看一区 | 国产chinesehd精品91 | 精品一区二区久久久久久久网精 | 精品国产一区二区三区四区在线 | 欧美成人精品一区二区三区 | 国产成人精品免费视频大全最热 | 操你啦免费视频 | 鲁丝一区二区二区四区 | 国产一区二区免费在线观看视频 | 中国老女人一级毛片视频 | 久久艹综合 | 澳门一级淫片免费视频 | 国产精品99久久久久久大便 | 亚洲成人中文字幕在线 | 国产99久久精品一区二区 | 国产一区二区三区视频在线 | 成人免费观看av | 国产69精品久久久久孕妇黑 | 午夜视频观看 | 黑色丝袜美美女被躁视频 | 国产精品久久久久永久免费 | 最近日本电影hd免费观看 | 免费一及片 | 欧美精品欧美 | 视频一区 在线 | 91午夜在线观看 | 日本羞羞影院 | 最新在线黄色网址 | 香蕉成人在线视频 | 亚洲男人天堂 | 午夜精品福利在线观看 | 国产永久免费观看 | 毛片免费视频观看 | 怦然心动50免费完整版 | 龙床上的呻吟高h | 黄色电影免费提供 | 香蕉成人在线视频 | 毛片免费在线观看视频 | 毛片免费观看完整版 |