ActivityGroup实现Android仿ios底部导航效果

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了ActivityGroup实现Android仿ios底部导航效果脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

今天接到项目经理任务,做一个底部导航,现在主流的而且推荐的做法都是Tabhost+ActivITyGrouP实现,但是项目经理说,为了便于维护,用ActivityGroup实现- 囧 -
最终效果图:
请输入图片描述
码:
activity_main_group.XMl

?xML version="1.0" encoding="utf-8"?>
<RelativeLayout >"http://schemas.andROId.COM/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout                                //该布局用于动态加载View显示在上图的空白区域中              
    android:id="@+id/ll_main_bodyer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/ll_main_bottom"
    android:orientation="vertical" >
</LinearLayout>

<LinearLayout
    android:id="@+id/ll_main_bottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/bg_main_bottom"
    android:orientation="horizontal"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" >

        <ImageView                           //用于显示按钮点击或获得焦点以后显示的白色阴影
            android:id="@+id/img_main_homepage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_homepage_bottom_clicked"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_main_homepage_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true"
            android:clickable="true"
            android:focusable="true"
            android:drawableTop="@drawable/icon_main_bottom_homepage_2"
            android:text="@string/tv_main_bottom_homepage"
            android:textColor="@android:color/white" />
    </RelativeLayout>

    <RelativeLayout
       android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/img_main_coupon"
         android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:background="@drawable/bg_homepage_bottom_clicked"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_main_coupon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:clickable="true"
            android:focusable="true"
            android:drawableTop="@drawable/icon_main_bottom_coupon" //drawableTop属性用于将图片显示在View上方
            android:text="@string/tv_main_bottom_coupon"
            android:textColor="@android:color/white" />
    </RelativeLayout>

    <RelativeLayout
       android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/img_main_PErson"
           android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:background="@drawable/bg_homepage_bottom_clicked"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_main_person"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawableTop="@drawable/icon_main_bottom_person"
            android:text="@string/tv_main_bottom_person"
            android:textColor="@android:color/white" />
    </RelativeLayout>

    <RelativeLayout
       android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/img_main_more"
          android:layout_width="match_parent"
        android:layout_height="match_parent"
            android:background="@drawable/bg_homepage_bottom_clicked"
            android:visibility="gone" />

        <TextView
            android:id="@+id/tv_main_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:drawableTop="@drawable/icon_main_bottom_more"
            android:text="@string/tv_main_bottom_more"
            android:textColor="@android:color/white" />
    </RelativeLayout>
</LinearLayout>

MainActivity.java:

public class MainActivity extends ActivityGroup implements OnClickListener {
LinearLayout ll_bodyer;
PRivate TextView tv_homepage, tv_more, tv_coupon, tv_person;
private ImageView iv_homepage,iv_more,iv_coupon,iv_person;
@override
protected void onCreate(Bundle savedInstancestate) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_group);
    initBodyer();
    initBottom();
}

private void initBodyer() {
    ll_bodyer = (LinearLayout) findViewById(R.id.ll_main_bodyer); 
}

private void initBottom() {
    tv_homepage = (TextView) findViewById(R.id.tv_main_homepage_2);
    tv_more = (TextView) findViewById(R.id.tv_main_more);
    tv_coupon = (TextView) findViewById(R.id.tv_main_coupon_2);
    tv_person = (TextView) findViewById(R.id.tv_main_person_2);

    iv_homepage=(ImageView) findViewById(R.id.img_main_homepage);
    iv_more=(ImageView) findViewById(R.id.img_main_more);
    iv_coupon=(ImageView) findViewById(R.id.img_main_coupon);
    iv_person=(ImageView) findViewById(R.id.img_main_person);

    tv_homepage.setOnClickListener(this);
    tv_more.setOnClickListener(this);
    tv_coupon.setOnClickListener(this);
    tv_person.setOnClickListener(this);
    tv_homepage.performClick();
}

@Override
public void onClick(View v) {
    Intent intent = new Intent();
    Bundle bundle = new Bundle();
    setFouces(v.getId());          //该方法用于判断TextView是否获得点击获得焦点
    switch (v.getId()) {           //因为Acivity实现了OnClickListener方法,可用此方法处理点击事件,博客末尾会说明原因
    case R.id.tv_main_homepage_2:
        ll_bodyer.removeAllViews();
        intent.setClass(MainActivity_2.this, HomePageActivity_new_2.class);
        View view_1 = getLocalActivityManager().startActivity(v.getId() + "", intent).getDecorView();                       //用于加载页面中空白的那个LinearLayout所包含的View
        view_1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));//设置加载的View属性
        view_1.dispatchWindowFocusChanged(true);//暂时不明白此方法的用处..
        ll_bodyer.addView(view_1);
        break;
    case R.id.tv_main_coupon_2:
        ll_bodyer.removeAllViews();
        bundle.putString("sign", "1");
        intent.putExtras(bundle);
        intent.setClass(this, CouponActivity.class);
        View view_2 = getLocalActivityManager().startActivity(v.getId() + "", intent).getDecorView();
        view_2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        view_2.dispatchWindowFocusChanged(true);
        ll_bodyer.addView(view_2);
        break;
    case R.id.tv_main_person_2:
        //....
        break;
    case R.id.tv_main_more:
        //....
        break;

    }
}

关于implements OnClickListener用法原理以及与其它监听器的区别:
http://stackoverflow.com/questions/17540013/declaring-that-a-class-implements-onclicklistener-vs-declaring-it-yourself
请输入图片描述
补充:当我把这个布局拿到别的手机上去测试的时候发现,有的手机并不能达到标题图下所展示的效果,也就是屏幕适配的问题,于是又回来改了下,改动就是 将底部的每一个RelativeLayout和ImageView的layout_width和layout_height都设置为match_parent,也就是全部填充父窗口。
OK啦,至于Tabhost+Fragment以后我会自己去实现下后贴到博客上的。

脚本宝典总结

以上是脚本宝典为你收集整理的ActivityGroup实现Android仿ios底部导航效果全部内容,希望文章能够帮你解决ActivityGroup实现Android仿ios底部导航效果所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。