Android筆記之FragmentTabHost實現選項卡


FragmentTabHost

API:http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

1、main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <!-- tabhost裝載5個tab的容器 -->
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

2、FragmentActivity中使用fragmenttabhost

(1)TabSpec的實例化:TabSpec tabSpec = mTabHost.newTabSpec(String tag).setIndicator(.....);

  • setIndicator(View view);   //Specify a view as the tab indicator.
setIndicator(getTabItemView())
private View getTabItemView(int index){
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);
    
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        imageView.setImageResource(R.drawable.tab_home_btn);
        
        TextView textView = (TextView) view.findViewById(R.id.textview);        
        textView.setText("首頁");
    
        return view;
    }
  • setIndicator(CharSequence label, Drawable icon)  //Specify a label and icon as the tab indicator. label即為選項卡的顯示的文本
setIndicator("Android",getResources().getDrawable(R.drawable.icon_home_nor));       
  • setIndicator( CharSequence label)  //Specify a label as the tab indicator.
setIndicator("設置")

主要代碼:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

/**
 * This demonstrates how you can implement switching between the tabs of a
 * TabHost through fragments, using FragmentTabHost.
 */
public class MainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("首頁").setIndicator("Simple"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("分類") .setIndicator("Contacts"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("排行").setIndicator("Custom"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("熱門") .setIndicator("Throttle"), fragment_1.class, null); mTabHost.addTab(mTabHost.newTabSpec("設置") .setIndicator("Throttle"), fragment_1.class, null); } }
fragment_X.class的代碼
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class fragment_1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_1, container, false);
        return v;
    }
}

3、fragment下使用fragmentabhost

 (1)fragment_1本身不需要布局文件

(2)選項卡默認在頂部

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;public class fragment_1 extends Fragment {
    private FragmentTabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment1/59);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                subfragment_1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                subfragment_1.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                subfragment_1.class, null);
        return mTabHost;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTabHost = null;
    }
}

 

 

4、其他函數

mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);

Done!

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM