创建 居于屏幕下方的tabhost,动态更换tab的背景图片和字体颜色

话说这个东西耗费了我一上午的时间。经过查阅资料和自己的一些想法。终于搞定了。下面是代码。希望能和大家一起学习。我是没学几天的新手。希望大神勿喷啊。

MainActivity.java

package com.example.pwdmanaget;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {
    private String ACTIVITY_NAME = "B";
    private LinearLayout ll;
    private Button btn_exit;
    private SharedPreferences sp;
    private int bg_info;
    private int bc_count;
    private int[] unSelectedTabIcons = {R.drawable.menu_icon_0_normal,R.drawable.menu_icon_1_normal,R.drawable.menu_icon_2_normal,R.drawable.menu_icon_3_normal};
    private int[] selectedTabIcons = {R.drawable.menu_icon_0_pressed,R.drawable.menu_icon_1_pressed,R.drawable.menu_icon_2_pressed,R.drawable.menu_icon_3_pressed};
    private String[] menuText = {"menu1","menu2","menu3"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll = (LinearLayout)findViewById(R.id.main_ll);
        btn_exit = (Button)findViewById(R.id.btn_exit);
        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                //SysApplication.exit();
            }
        });
        
        sp = getSharedPreferences("PwdManaget", 0);
        bg_info = sp.getInt("bg_info", R.drawable.bg);
        
        Resources resources = getResources();
        Drawable drawable = resources.getDrawable(bg_info);
        
        ll.setBackgroundDrawable(drawable);
        
        
        
        
        // 如果没有继承TabActivity时,通过该种方法加载启动tabHost  
        final TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
        tabHost.setup();  
        TabSpec tabSpec = tabHost.newTabSpec("tab1").setIndicator(getTabHost(0)).setContent(R.id.tab1);
        TabSpec tabSpec2 = tabHost.newTabSpec("tab2").setIndicator(getTabHost(1)).setContent(R.id.tab2);
        TabSpec tabSpec3 = tabHost.newTabSpec("tab2").setIndicator(getTabHost(2)).setContent(R.id.tab3);
        tabHost.addTab(tabSpec);
        tabHost.addTab(tabSpec2);
        tabHost.addTab(tabSpec3);
        
        View view = tabHost.getTabWidget().getChildAt(0);
        ImageView imageView = (ImageView)view.findViewById(0);
        imageView.setImageDrawable(getResources().getDrawable(selectedTabIcons[0]));
        view.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_btn_bg));
        
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            
            @Override
            public void onTabChanged(String tabId) {
                updateTab(tabHost);
            }
            
            
        });
    }
    private View getTabHost(int i) {
        LinearLayout view = new LinearLayout(this);
        view.setOrientation(LinearLayout.VERTICAL);
        view.setBackgroundDrawable(getResources().getDrawable(R.drawable.bottom1));
        
        ImageView iv = new ImageView(this);
        iv.setId(i);
        iv.setImageDrawable(getResources().getDrawable(unSelectedTabIcons[i]));
        
        TextView tv = new TextView(this);
        tv.setText(menuText[i]);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setTextColor(Color.parseColor("#FFFFFF"));
        tv.setId(0x0010+i);
        
        view.addView(iv);
        view.addView(tv);
        
        return view;
    }
    private void updateTab(TabHost tabHost) {
        int curr = tabHost.getCurrentTab();
        View view = tabHost.getTabWidget().getChildAt(curr);
        view.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_btn_bg));
        
        ImageView imageView = (ImageView)view.findViewById(curr);
        imageView.setImageDrawable(getResources().getDrawable(selectedTabIcons[curr]));
        
        TextView textView = (TextView)view.findViewById(0x0010+curr);
        textView.setTextColor(Color.parseColor("#FFFFFF"));
        
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            if(i!=curr){
                View curr_view = tabHost.getTabWidget().getChildAt(i);
                curr_view.setBackgroundDrawable(getResources().getDrawable(R.drawable.bottom1));
                
                
                ImageView curr_imageView = (ImageView)curr_view.findViewById(i);
                curr_imageView.setImageDrawable(getResources().getDrawable(unSelectedTabIcons[i]));
                TextView curr_textView = (TextView)curr_view.findViewById(0x0010+i);
                curr_textView.setTextColor(Color.parseColor("#E0E0E0"));
            }
        }
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(ACTIVITY_NAME, "onstart!");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(ACTIVITY_NAME, "onRestart!");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.i(ACTIVITY_NAME, "onResume!");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.i(ACTIVITY_NAME, "onPause!");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(ACTIVITY_NAME, "onStop!");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(ACTIVITY_NAME, "onDestroy!");
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Log.i("count", bc_count+"");
            if(bc_count==0){
                Toast.makeText(MainActivity.this, "在按一次退出", Toast.LENGTH_SHORT).show();
                new Thread(){
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }finally{
                            bc_count = 0;
                            Log.i("count", bc_count+"");
                        }
                    };
                }.start();
                bc_count++;
            }else{
                finish();
            }
        }
        return false;
    }
    
    
}

activity_main.xml布局文件

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical"
    android:id="@+id/main_ll"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="@drawable/bottom1"
        >
        <Button
        android:id="@+id/btn_exit" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:onClick="exit()"
        android:text="退出"
        android:textColor="#ffffff"
        android:drawableLeft="@drawable/bt_back"
        />
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:text="密码管家"
            android:textSize="20sp"
            android:textColor="#ffffff"
            
            />
    </RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        >
        <TabHost
            android:id="@+id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            >
            <RelativeLayout
                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:layout_alignParentBottom="true"
                    />
                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" 
                        >
                        <LinearLayout
                            android:id="@+id/tab1" 
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:orientation="vertical"
                            >
                            <TextView
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:text="this is tab1 content" 
                                />
                        </LinearLayout>
                        <LinearLayout
                            android:id="@+id/tab2" 
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:orientation="vertical"
                            >
                            <TextView
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:text="this is tab2 content" 
                                />
                        </LinearLayout>
                        <LinearLayout
                            android:id="@+id/tab3" 
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:orientation="vertical"
                            >
                            <TextView
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content"
                                android:text="this is tab3 content" 
                                />
                        </LinearLayout>
                    </FrameLayout>
            </RelativeLayout>
        </TabHost>
    </LinearLayout>
</LinearLayout>

 

下面是完整代码的下载地址

http://pan.baidu.com/s/1dDowK1b

posted @ 2014-08-11 14:41  mr.g.  阅读(1956)  评论(0编辑  收藏  举报