zoukankan      html  css  js  c++  java
  • 几种tabhost的总结(2)

    底背景图标动画切换

    一。布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TabHost
            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:orientation="vertical" >
    
                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="0.0dip"
                    android:layout_weight="1.0" >
                </FrameLayout>
    
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone" />
    
                <RelativeLayout
                    android:id="@+id/layout_bottom"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >
    
                    <RadioGroup
                        android:id="@+id/radiogroup"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="bottom"
                        android:background="@drawable/bottombg"
                        android:gravity="center_vertical"
                        android:orientation="horizontal" >
    
                        <RadioButton
                            android:id="@+id/radio_news"
                            android:layout_width="wrap_content"
                            android:background="@drawable/tab_selector_news"
                            android:button="@null"
                            android:checked="true" />
    
                        <RadioButton
                            android:id="@+id/radio_topic"
                            android:layout_width="wrap_content"
                            android:background="@drawable/tab_selector_topic"
                            android:button="@null" />
    
                        <RadioButton
                            android:id="@+id/radio_pic"
                            android:layout_width="wrap_content"
                            android:background="@drawable/tab_selector_pic"
                            android:button="@null" />
    
                        <RadioButton
                            android:id="@+id/radio_follow"
                            android:layout_width="wrap_content"
                            android:background="@drawable/tab_selector_follow"
                            android:button="@null" />
    
                        <RadioButton
                            android:id="@+id/radio_vote"
                            android:layout_width="wrap_content"
                            android:background="@drawable/tab_selector_vote"
                            android:button="@null" />
                    </RadioGroup>
                </RelativeLayout>
            </LinearLayout>
        </TabHost>
    
    </LinearLayout>

    二。activity

    package com.ct.host;
    
    import android.app.TabActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.TranslateAnimation;
    import android.widget.ImageView;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.RelativeLayout;
    import android.widget.TabHost;
    
    public class MainActivity extends TabActivity {
        /** Called when the activity is first created. */
        TabHost tabHost;
        TabHost.TabSpec tabSpec;
        RadioGroup radioGroup;
        RelativeLayout bottom_layout;
        ImageView img;
        int startLeft;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            bottom_layout = (RelativeLayout) findViewById(R.id.layout_bottom);
            
            tabHost = getTabHost();
            tabHost.addTab(tabHost.newTabSpec("news").setIndicator("News").setContent(new Intent(this, TabNewsActivity.class)));
            tabHost.addTab(tabHost.newTabSpec("topic").setIndicator("Topic").setContent(new Intent(this, TabTopicActivity.class)));
            tabHost.addTab(tabHost.newTabSpec("picture").setIndicator("Picture").setContent(new Intent(this, TabPicActivity.class)));
            tabHost.addTab(tabHost.newTabSpec("follow").setIndicator("Follow").setContent(new Intent(this, TabFollowActivity.class)));
            tabHost.addTab(tabHost.newTabSpec("vote").setIndicator("Vote").setContent(new Intent(this, TabVoteActivity.class)));
            
            radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
            radioGroup.setOnCheckedChangeListener(checkedChangeListener);
            
            img = new ImageView(this);
            img.setImageResource(R.drawable.tab_front_bg);
            bottom_layout.addView(img);
        }
        
        private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener(){
    
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                switch (checkedId) {
                case R.id.radio_news:
                    tabHost.setCurrentTabByTag("news");
    //                moveFrontBg(img, startLeft, 0, 0, 0);
                    moveFrontBg(img, startLeft, 0, 0, 0);
                    startLeft = 0;
                    break;
                case R.id.radio_topic:
                    tabHost.setCurrentTabByTag("topic");
                    moveFrontBg(img, startLeft, img.getWidth(), 0, 0);
                    startLeft = img.getWidth();
                    break;
                case R.id.radio_pic:
                    tabHost.setCurrentTabByTag("picture");
                    moveFrontBg(img, startLeft, img.getWidth() * 2, 0, 0);
                    startLeft = img.getWidth() * 2;
                    break;
                case R.id.radio_follow:
                    tabHost.setCurrentTabByTag("follow");
                    moveFrontBg(img, startLeft, img.getWidth() * 3, 0, 0);
                    startLeft = img.getWidth() * 3;
                    break;
                case R.id.radio_vote:
                    tabHost.setCurrentTabByTag("vote");
                    moveFrontBg(img, startLeft, img.getWidth() * 4, 0, 0);
                    startLeft = img.getWidth() * 4;
                    break;
    
                default:
                    break;
                }
            }
            
        };
        
        private void  moveFrontBg(View v, int startX, int toX, int startY, int toY){
            TranslateAnimation anim = new TranslateAnimation(startX, toX, startY, toY);
            anim.setDuration(200);
            anim.setFillAfter(true);
            v.setAnimation(anim);
            
        }
        
    }

    (在F:\java\MyTabHost2)

  • 相关阅读:
    【C#进阶系列】06 类型和成员基础
    纪中5日T1 1564. 旅游
    纪中17日T1 2321. 方程
    纪中17日T2 2322. capacitor
    纪中10日T1 2313. 动态仙人掌
    纪中14日听课小结 图论 最短路 二分图 差分约束
    一个抓猫的游戏 消遣GAME 持续更新中!
    洛谷P1464 Function  HDU P1579 Function Run Fun
    洛谷P1976 鸡蛋饼
    纪中12日T1 2307. 选择
  • 原文地址:https://www.cnblogs.com/ct732003684/p/2849359.html
Copyright © 2011-2022 走看看