您的位置:首页 > 其它

Tabhost漂亮的自定义实现(背景随着选项卡滑动改变)

2013-05-16 14:50 555 查看
在网上找了许多资料都找不到自己想要的。

结果自己实现:

布局文件不帖出来了:

外层一个TabHost: 里面放《Linearlayout》〈TabWidget></Tabwidget><Framlayout></Framlayout></Linearlayout>

一:继承Tabhost

主代码:

tabhost=getTabHost();
final Resources resources=getResources();

//主页
TabHost.TabSpec specHome=tabhost.newTabSpec("home");
specHome.setIndicator("",resources.getDrawable(android.R.drawable.ic_menu_myplaces));
home =new Intent(MainActivity.this,HomeActivity.class);
specHome.setContent(home);
tabhost.addTab(specHome);

//搜索
TabHost.TabSpec specSearch=tabhost.newTabSpec("search");
specSearch.setIndicator("",resources.getDrawable(android.R.drawable.ic_menu_search));
search=new Intent(MainActivity.this,SearchActivity.class);
specSearch.setContent(search);
tabhost.addTab(specSearch);

// 分类
TabHost.TabSpec specClassify =tabhost.newTabSpec("classify");
specClassify.setIndicator("",resources.getDrawable(android.R.drawable.ic_menu_week));
classify=new Intent(MainActivity.this,ClassifyActivity.class);
specClassify.setContent(classify);
tabhost.addTab(specClassify);

//购物车
TabHost.TabSpec specShoppingCart=tabhost.newTabSpec("shoppingCart");
specShoppingCart.setIndicator("",resources.getDrawable(id))

//百宝箱
TabHost.TabSpec specTreasureChest=tabhost.newTabSpec("treasureChest");
specTreasureChest.setIndicator("",resources.getDrawable(android.R.drawable.ic_dialog_dialer));
treasureChest=new Intent(MainActivity.this, TreasureChestActivity.class);
specTreasureChest.setContent(treasureChest);
tabhost.addTab(specTreasureChest);

tabhost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {

Activity activity=getLocalActivityManager().getActivity(tabId);
if(activity!=null)
activity.onWindowFocusChanged(true);

}

});

简单的实现到这里结束:
===========
(二)
下面的讲解由许多配置文件完成:

Styles.xml文件
---

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="main_tab_bottom">
<item name="android:textSize">@dimen/bottom_tab_font_size</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/home_btn_bg</item>
<item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
<item name="android:paddingBottom">2.0dip</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginBottom">2.0dip</item>
<item name="android:button">@null</item>
<item name="android:singleLine">true</item>
<item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
<item name="android:layout_weight">1.0</item>
</style>
</resources>

ids.xml文件
---

<resources>
<item type="id" name="main_radio">false</item>
<item type="id" name="radio_button0">false</item>
<item type="id" name="radio_button1">false</item>
<item type="id" name="radio_button2">false</item>
<item type="id" name="radio_button3">false</item>
<item type="id" name="radio_button4">false</item>
</resources>

dimens.xml文件
---

<resources>
<dimen name="bottom_tab_font_size">10.0sp</dimen>
<dimen name="bottom_tab_padding_up">5.0dip</dimen>
<dimen name="bottom_tab_padding_drawable">2.0dip</dimen>
<dimen name="switch_logo_bottom_padding">30.0sp</dimen>
<dimen name="widget_height">100.0dip</dimen>
<dimen name="sta_height">48.0dip</dimen>
<dimen name="large_padding_length">20.0dip</dimen>
<dimen name="widget_write_margin_top">19.0dip</dimen>
<dimen name="widget_write_margin_left">10.0dip</dimen>
<dimen name="widget_content_margin_top">20.0dip</dimen>
<dimen name="widget_content_margin_left">10.0dip</dimen>
<dimen name="widget_logo_size">35.0dip</dimen>
<dimen name="title_height">74.0dip</dimen>
<dimen name="new_blog_size">70.0dip</dimen>
<dimen name="emotion_item_view_height">13.299988dip</dimen>
<dimen name="splash_test_top_margin_top">20.0dip</dimen>
<dimen name="splash_test_center_margin_right">0.0dip</dimen>
<dimen name="title_text_size">20.0sp</dimen>
<dimen name="normal_padding_length">10.0dip</dimen>
<dimen name="no_result_padding_length">50.0dip</dimen>
</resources>

drawables.xml文件
---

<resources>
<item type="drawable" name="ltgray">#fff4f4f4</item>
<item type="drawable" name="ltyellow">#fffff4db</item>
<item type="drawable" name="black">#ff000000</item>
<item type="drawable" name="transparent">#00000000</item>
<item type="drawable" name="widget_edit_block_bg_normal">@android:color/transparent</item>
<item type="drawable" name="transparent_background">#99000000</item>
<item type="drawable" name="list_background">#fff4f4f4</item>
<item type="drawable" name="namcard_picker_bkg_normal">#ff272727</item>
<item type="drawable" name="namcard_picker_bkg_hover">#ff333333</item>
</resources>

home_btn_bg.xml文件
---

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_s" />
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_s" />
<item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" />
<item android:drawable="@drawable/transparent" />
</selector>

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"
android:background="@color/bisque">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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:visibility="gone"

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0" />
<RadioGroup android:gravity="center_vertical"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:id="@id/main_radio"
android:background="@drawable/maintab_toolbar_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton android:id="@id/radio_button0" android:layout_marginTop="2.0dip" android:text="@string/main_home" android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@id/radio_button1" android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@id/radio_button2" android:layout_marginTop="2.0dip" android:text="@string/main_manage_date" android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@id/radio_button3" android:layout_marginTop="2.0dip" android:text="@string/main_friends" android:drawableTop="@drawable/icon_4_n" style="@style/main_tab_bottom" />
<RadioButton android:id="@id/radio_button4" android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" style="@style/main_tab_bottom" />
</RadioGroup>
</LinearLayout>
</TabHost>

接下来是======〉代码部分
MainActivity:

package cn.activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;

public class MainActivity extends TabActivity implements OnCheckedChangeListener{

private TabHost tabhost=null;

private Intent home;
private Intent search;
private Intent classify;
private Intent shoppingCart;
private Intent treasureChest;

private RadioButton radio_btn0,radio_btn1,radio_btn2,radio_btn3,radio_btn4;
void init(){
radio_btn0=(RadioButton) findViewById(R.id.radio_button0);
radio_btn1=(RadioButton) findViewById(R.id.radio_button1);
radio_btn2=(RadioButton) findViewById(R.id.radio_button2);
radio_btn3=(RadioButton) findViewById(R.id.radio_button3);
radio_btn4=(RadioButton) findViewById(R.id.radio_button4);
}

void setUp(){

this.home=new Intent(this, HomeActivity.class);
this.search=new Intent(this,SearchActivity.class);
this.classify=new Intent(this,ClassifyActivity.class);
this.shoppingCart=new Intent(this, ShoppingCartActivity.class);
this.treasureChest=new Intent(this ,TreasureChestActivity.class);

radio_btn0.setOnCheckedChangeListener(this);
radio_btn1.setOnCheckedChangeListener(this);
radio_btn2.setOnCheckedChangeListener(this);
radio_btn3.setOnCheckedChangeListener(this);
radio_btn4.setOnCheckedChangeListener(this);
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
setUp();
setupIntent();
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
switch (buttonView.getId()) {
case R.id.radio_button0:
this.tabhost.setCurrentTabByTag("home");
break;
case R.id.radio_button1:
this.tabhost.setCurrentTabByTag("search");
break;
case R.id.radio_button2:
this.tabhost.setCurrentTabByTag("classify");
break;
case R.id.radio_button3:
this.tabhost.setCurrentTabByTag("shoppingCart");
break;
case R.id.radio_button4:
this.tabhost.setCurrentTabByTag("treasureChest");
break;
}
}
}

private void setupIntent(){
this.tabhost=getTabHost();
TabHost localTabHost=this.tabhost;
localTabHost.addTab(setTabSpec("home",R.string.main_home, R.drawable.icon_1_n, this.home));
localTabHost.addTab(setTabSpec("search",R.string.main_search, R.drawable.icon_2_n, this.search));
localTabHost.addTab(setTabSpec("classify",R.string.main_classify, R.drawable.icon_3_n, this.classify));
localTabHost.addTab(setTabSpec("shoppingCart",R.string.main_shoppingcart, R.drawable.icon_4_n, this.shoppingCart));
localTabHost.addTab(setTabSpec("treasureChest",R.string.main_treasure_chest, R.drawable.icon_5_n, this.treasureChest));
}

/**
* example:
* TabHost.TabSpec specHome=tabhost.newTabSpec("home");
* specHome.setIndicator("",resources.getDrawable(android.R.drawable.ic_menu_myplaces));
* home =new Intent(MainActivity.this,HomeActivity.class);
* specHome.setContent(home);
* tabhost.addTab(specHome);
* @param tag
* @param resLabel
* @param resIcon
* @param content
* @return
*/
private TabHost.TabSpec setTabSpec(String tag,int resLabel,int resIcon,final Intent content){
return this.tabhost.newTabSpec(tag).setIndicator(getString(resLabel),getResources().getDrawable(resIcon)).setContent(content);
}
}

====================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: