【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;
}
}