BeeFramework框架学习之三(底部菜单栏的实现之一)

来源:互联网 发布:sql scope identity 编辑:程序博客网 时间:2024/06/07 15:04

一、效果图:
这里写图片描述
这里写图片描述
二、主要代码:
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="match_parent"    android:layout_height="match_parent"    android:background="#ffffff" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <TabWidget            android:id="@android:id/tabs"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:visibility="gone" />        <!-- 这个方法是使用TabHost必须带的 -->        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="fill_parent"            android:layout_height="match_parent"            android:layout_weight="1" >        </FrameLayout>        <FrameLayout            android:layout_width="fill_parent"            android:layout_height="40dp" >            <RadioGroup                android:id="@+id/tab_group"                android:layout_width="fill_parent"                android:layout_height="40dp"                android:orientation="horizontal" >                <RadioButton                    android:id="@+id/tab_one"                    android:layout_width="match_parent"                    android:layout_height="40dp"                    android:layout_weight="1"                    android:background="@drawable/tab_select"                    android:button="@null"                    android:checked="true"                    android:gravity="center"                    android:text="TabHost1"                    android:textColor="#000000"                    android:textSize="10dp" />                <RadioButton                    android:id="@+id/tab_two"                    android:layout_width="match_parent"                    android:layout_height="40dp"                    android:layout_weight="1"                    android:background="@drawable/tab_select"                    android:button="@null"                    android:gravity="center"                    android:text="TabHost2"                    android:textColor="#000000"                    android:textSize="10dp" />                <RadioButton                    android:id="@+id/tab_three"                    android:layout_width="match_parent"                    android:layout_height="40dp"                    android:layout_weight="1"                    android:background="@drawable/tab_select"                    android:button="@null"                    android:gravity="center"                    android:text="TabHost3"                    android:textColor="#000000"                    android:textSize="10dp" />            </RadioGroup>            <ImageView                android:id="@+id/home_tab_image"                android:layout_width="70dp"                android:layout_height="40dp"                android:background="#44000000" />        </FrameLayout>    </LinearLayout></TabHost>

MainActivity.class主要代码:

private void ChangeImageSize() {        width=getWindowManager().getDefaultDisplay().getWidth()/3;        image=(ImageView)findViewById(R.id.home_tab_image);        LayoutParams params=(LayoutParams)image.getLayoutParams();        params.width=width;        image.setLayoutParams(params);    }
private void getMyTabHost() {        tabhost=getTabHost();        TabHost.TabSpec spec_tab1=tabhost.newTabSpec("spec_tab1").setIndicator("spec_tab1")                .setContent(new Intent(MainActivity.this,FirstActivity.class));        tabhost.addTab(spec_tab1);        TabHost.TabSpec spec_tab2=tabhost.newTabSpec("spec_tab2").setIndicator("spec_tab2")                .setContent(new Intent(MainActivity.this,SecondActivity.class));        tabhost.addTab(spec_tab2);        TabHost.TabSpec spec_tab3=tabhost.newTabSpec("spec_tab3").setIndicator("spec_tab3")                .setContent(new Intent(MainActivity.this,ThirdActivity.class));        tabhost.addTab(spec_tab3);        radioGroup();    }
private void radioGroup() {        RadioGroup group=(RadioGroup)this.findViewById(R.id.tab_group);        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                switch (checkedId) {                case R.id.tab_one:                    tabhost.setCurrentTabByTag("spec_tab1");                    mTranslateAnimation=new TranslateAnimation(start, 0,0,0);                    mTranslateAnimation.setDuration(200);                    mTranslateAnimation.setFillEnabled(true);                    mTranslateAnimation.setFillAfter(true);                    image.startAnimation(mTranslateAnimation);                    start=0;                    break;                case R.id.tab_two:                    tabhost.setCurrentTabByTag("spec_tab2");                    mTranslateAnimation=new TranslateAnimation(start, width,0,0);                    mTranslateAnimation.setDuration(200);                    mTranslateAnimation.setFillEnabled(true);                    mTranslateAnimation.setFillAfter(true);                    image.startAnimation(mTranslateAnimation);                    start=width;                    break;                case R.id.tab_three:                    tabhost.setCurrentTabByTag("spec_tab3");                    mTranslateAnimation=new TranslateAnimation(start, width*2,0,0);;                    mTranslateAnimation.setDuration(200);                    mTranslateAnimation.setFillEnabled(true);                    mTranslateAnimation.setFillAfter(true);                    image.startAnimation(mTranslateAnimation);                    start=width*2;                    break;                }            }        });    }

三、相关代码讲解:

xml中注意的地方:
1.TabHost作为最外面的布局,默然必须有TabWidget标签,
这里不需要,android:visibility=”gone”把它隐藏掉。
2.FrameLayout中使用layout_weight来控制大小。
3.RadioGroup和RadioButton组件来完成下侧菜单栏的实现。
4.ImageView实现一个动画的效果。

MainActivity.class中讲解的地方:
1.ChangeImageSize()方法实现的是改变ImageView的大小,这里只是改变其宽度width.使用LayoutParams 对象来设置ImageView的参数width,这里width=getWindowManager.getDefaultDisplay.getWidth()/3;
图片image通过setLayoutParams(LayoutParams params)来更改大小。

2.getMyTabHost()方法通过申明一个TabHost对象的容器,添加相应的TabHost到里面。
申明一个TabHost对象:TabHost tabhost=getTabHost();
添加相关的TabHost:
TabHost.TabSpec spec=tabhost.newTabSpec(“spec_tab”).setIndicator(“spec_tab”)
.setContent(new Intent(this,XXActivity));

tabhost.addTab(spec);

3.radioGroup()方法讲解:
因为底部菜单栏使用RadioGroup和RadioButton实现的,这里主要是获取相关的空间并设置监听事件。
RadioGroup的监听事件使用setOnCheckedChangeListener监听,
默认方法是onCheckedChanged(RadioGroup group, int checkedId);
这里使用switch来进行切换:
其中注意:setCurrentTabByTag(String tag)中tag对应的是上面设置的newTabSpec(String tag);
并执行相关的切换动画:
mTranslateAnimation=new TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);
四个参数的含义:
float fromXDelta 动画开始的点离当前View X坐标上的差值
float toXDelta 动画结束的点离当前View X坐标上的差值
float fromYDelta 动画开始的点离当前View Y坐标上的差值
float toYDelta 动画开始的点离当前View Y坐标上的差值
这里只是对x进行改变,y值并不发生改变,所以都为0,0;

setDuration(long durationMillis);//设置动画持续时间
setFillEnabled(setFillEnabled)//填充效果
setFillAfter(boolean fillAfter)//终止填充

四、相关源码下载地址:
http://pan.baidu.com/s/1kT4CQ9p

0 0