自定义FragmentTabHost样式切换Fragment

【1】布局文件中使用

  • ImageView  是加号覆盖的显示位置 

     

  •  android:id="@android:id/tabhost"2

     

btn_quickoption_selector

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_quickoption_nor" android:state_focused="false" android:state_pressed="false"/>

    <item android:drawable="@drawable/btn_quickoption_pressed" android:state_pressed="true"/>

</selector>
  • 布局文件使用

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <FrameLayout

        android:id="@+id/fl"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1" />

    <FrameLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

        <android.support.v4.app.FragmentTabHost

            android:id="@android:id/tabhost"

            android:layout_width="match_parent"

            android:layout_height="60dp"

            android:background="@color/white" />

        <ImageView

            android:id="@+id/iv_image"

            android:layout_gravity="center"

            android:background="@drawable/btn_quickoption_selector"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

    </FrameLayout>

</LinearLayout>

【2】实现FragmentTabHost切换Fragment关联切换

  • tab_indicator

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"

    android:gravity="center"

    android:paddingTop="10dp"

    android:layout_height="60dp">





    <ImageView

        android:id="@+id/iv_image"

        android:layout_width="20dp"

        android:layout_height="20dp" />





    <TextView

        android:id="@+id/tv_title"

        android:layout_marginTop="3dp"

        android:textColor="@color/viewpage_selector_slide_title"

        android:textSize="14sp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />





</LinearLayout>
  • 实现代码 

public class MainActivity2 extends AppCompatActivity {

    @Bind(R.id.fl)

    FrameLayout fl;

    @Bind(R.id.iv_image)

    ImageView iv_image;

    @Bind(android.R.id.tabhost)

    FragmentTabHost tabhost;





    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);

        ButterKnife.bind(this);





        //1.绑定tabhost和切换Fragment的布局

        tabhost.setup(this,getSupportFragmentManager(),R.id.fl);

        //去掉线

        tabhost.getTabWidget().setDividerDrawable(null);





        //2.创建Tab按钮

        //创建动弹界面的按钮

        TabHost.TabSpec allTab = tabhost.newTabSpec("all");D

        allTab.setIndicator(getIndicateView(R.drawable.tab_icon_new,"综合"));

        //创建动弹界面的按钮

        TabHost.TabSpec tweetTab = tabhost.newTabSpec("tweet");

        tweetTab.setIndicator(getIndicateView(R.drawable.tab_icon_tweet,"动弹"));

        //创建加号的按钮

        TabHost.TabSpec addTab = tabhost.newTabSpec("addTab");

        View addView = getIndicateView(0, "");

        addView.setEnabled(false);//禁用点击事件

        addTab.setIndicator(addView);

        //创建发现界面的按钮

        TabHost.TabSpec exploreTab = tabhost.newTabSpec("exploreTab");

        exploreTab.setIndicator(getIndicateView(R.drawable.tab_icon_explore,"发现"));

        //创建我界面的按钮

        TabHost.TabSpec meTab = tabhost.newTabSpec("meTab");

        meTab.setIndicator(getIndicateView(R.drawable.tab_icon_me,"我"));

        //3.添加Tab按钮以及对应的Fragment

        tabhost.addTab(allTab, AllFragment.class,null);

        tabhost.addTab(tweetTab, TweetFragment.class,null);

        tabhost.addTab(addTab, null,null);

        tabhost.addTab(exploreTab, ExploreFragment.class,null);

        tabhost.addTab(meTab, MeFragment.class,null);

        //给加号添加点击事件

        iv_image.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Toast.makeText(MainActivity2.this, "弹对话框!", Toast.LENGTH_SHORT).show();

            }

        });

    }

    private View getIndicateView(int iconResId,String title){

        View view = View.inflate(this,R.layout.tab_indicator,null);

        ImageView  iv_image = (ImageView) view.findViewById(R.id.iv_image);

        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);

        iv_image.setBackgroundResource(iconResId);

        tv_title.setText(title);

        return view;

    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴帅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值