1底部菜单的几种实现方式:
TabHost+Activity
RadioButton+Fragment
这种方法使用RadioGroup来实现RadioButton的互斥选择,而且需要在activity中实现和判断点击事件
FragmentTableHost+Fragment
这个就比较简单了,只需要在layout文件中定义fragmentTableHost(需要support v4包),然后再在文件中创建出tabspec并添加到fragmentTableHost中就可以了,比radiobutton少了监听点击事件和判断点击事件,因为直接就对view和fragment进行了绑定
2.fragmentTableHost的结构:
3.创建流程:
下面代码是大概流程
mTabHost= (FragmentTabHost) findViewById(android.R.id.tabhost);
//1.invoke setup method
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//2.create TabSpec
TabHost.TabSpec spec=mTabHost.newTabSpec("home");
//3.create a view as indicator
View view=null;
view=getLayoutInflater().inflate(R.layout.tab_indicator,null);
//initialize the child view of indicator view
ImageView img= (ImageView) view.findViewById(R.id.icon_tab);
TextView text= (TextView) view.findViewById(R.id.tv_indicator);
img.setBackgroundResource(R.mipmap.icon_home);
text.setText("主页");
//4.set indicator for spec
spec.setIndicator(view);
//5.add spec to fragmentTabHost
mTabHost.addTab(spec, Fragment.class, null);
4.具体实现
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".com.lc.store.activity.MainActivity">
<!--这个framelayout才是真正使用indicator填充的view,在这里对height使用weight-->
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:background="@color/bg_color">
</FrameLayout>
<!--注意,这里的id是使用android自带的id-"@android:id/tabhost"-->
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<!--这里的宽和高都是使用weight,而且id也是使用android的tabcontent-->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".com.lc.store.activity.MainActivity">
<!--这个framelayout才是真正使用indicator填充的view,在这里对height使用weight-->
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:background="@color/bg_color">
</FrameLayout>
<!--注意,这里的id是使用android自带的id-"@android:id/tabhost"-->
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<!--这里的宽和高都是使用weight,而且id也是使用android的tabcontent-->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
fragment布局文件:
这里列出其中一个
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/coral"
android:text="@string/home"/>
</LinearLayout>
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/coral"
android:text="@string/home"/>
</LinearLayout>
状态选择器:
这里只列出一个,其他的一样
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@mipmap/icon_home_press"/>
<item android:drawable="@mipmap/icon_home"/>
</selector>
MainActivity:
package com.lc.store.activity;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import com.lc.store.R;
import com.lc.store.activity.bean.Tab;
import com.lc.store.activity.fragment.CartFragment;
import com.lc.store.activity.fragment.CategoryFragment;
import com.lc.store.activity.fragment.HomeFragment;
import com.lc.store.activity.fragment.HotFragment;
import com.lc.store.activity.fragment.MineFragment;
import java.util.ArrayList;
import java.util.List;
/**
* FragmentTabHost structure:
* <p/>
* fragmentTabHost->tabSpec->indicator->view->ImageView
* ->TextView
* ->tabSpec
*/
public class MainActivity extends AppCompatActivity {
private FragmentTabHost mTabHost;
private List<Tab> mTabs = new ArrayList<Tab>(5);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
initTab();
}
private void initTab() {
Tab tab_home = new Tab(R.string.home, R.drawable.selector_icon_home, HomeFragment.class);
Tab tab_hot = new Tab(R.string.hot, R.drawable.selector_icon_hot, HotFragment.class);
Tab tab_category = new Tab(R.string.catagory, R.drawable.selector_icon_category, CategoryFragment.class);
Tab tab_cart = new Tab(R.string.cart, R.drawable.selector_icon_cart, CartFragment.class);
Tab tab_mine = new Tab(R.string.mine, R.drawable.selector_icon_mine, MineFragment.class);
mTabs.add(tab_home);
mTabs.add(tab_hot);
mTabs.add(tab_category);
mTabs.add(tab_cart);
mTabs.add(tab_mine);
//1.invoke setup method
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
for(Tab t:mTabs){
//2.create TabSpec
TabHost.TabSpec spec = mTabHost.newTabSpec(getString(t.getTitle()));
//3.create a view as indicator
View view = null;
view = getLayoutInflater().inflate(R.layout.tab_indicator, null);
//initialize the child view of indicator view
ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
TextView text = (TextView) view.findViewById(R.id.tv_indicator);
img.setBackgroundResource(t.getIcon());
text.setText(t.getTitle());
//4.set indicator for spec
spec.setIndicator(view);
//5.add spec to fragmentTabHost
mTabHost.addTab(spec, t.getFragment(), null);
}
//去掉分割线
mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
mTabHost.setCurrentTab(2);
}
}
fragment:
注意,这里继承的是v4里面的fragment
package com.lc.store.activity.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.lc.store.R;
/**
* Created by jiangbo on 2016/4/2.
* Description
*/
public class HomeFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home,container,false);
}
}