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

简介: 在网上找了许多资料都找不到自己想要的。 结果自己实现: 布局文件不帖出来了: 外层一个TabHost: 里面放《Linearlayout》〈TabWidget>  一:继承Tabhost     主代码:        tabhost=getTabHo...
在网上找了许多资料都找不到自己想要的。
结果自己实现:
布局文件不帖出来了:
外层一个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);
   } 
====================

相关文章
Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)
Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)
Qml实用技巧:在可视元素之前半透明覆盖一个可视元素,阻止鼠标透(界面)传(防止点击到被遮挡的按钮)
|
Android开发
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
641 0
Android 11 SystemUI(状态/导航栏)-状态栏下拉时图标的隐藏与通知面板的半透黑色背景
Qt隐藏系统标题栏,使用自定义标题栏
Qt隐藏系统标题栏,使用自定义标题栏
Qt隐藏系统标题栏,使用自定义标题栏
|
Dart 开发者
【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )(三)
【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )(三)
195 0
【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )(三)
【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )(一)
【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )(一)
136 0
【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )(二)
【Flutter】监听滚动动作 控制组件 透明度渐变 ( 移除顶部状态栏空白 | 帧布局组件 | 透明度组件 | 监听滚动组件 )(二)
224 0
|
Android开发
Android 自定义控件之SlidingMenuVertical顶部悬浮(垂直折叠抽屉,有滑动渐变回调,可自行添加渐变动画)
顶部悬浮(垂直折叠抽屉,有滑动渐变回调,可自行添加渐变动画)
2016 0
tablayout支持改变选中文字大小,支持左右滑动,支持viewpager,支持三角可移动指示器
TabLayout [简书地址] (https://www.jianshu.com/p/2c3f868266e8) 基于大神的FlycoTabLayout 传送地址和基本用法 用法和属性和这个库一样 效果图如下 Gif_20180828_142709.
2456 0
|
Android开发
【Android视图效果】仿QQ空间滑动改变标题栏颜色
最近在倒腾公司之前的项目,发现之前的界面是个白色标题栏,不是很美观,所以做了些改进。 先看效果图 165815uykp80g8y3goo5vz.gif 简单说下思路,整个布局大体上是ScrollView里面包含了一个ImageView和RecyclerView,所以先得到ImageView的高度,当ScrollView向上滑动时,设置标题栏的背景色、文字颜色,当超过ImageView的高度时,设置其背景为白色,字体为黑色。
1132 0