Android之Actionbar頂部標(biāo)簽的使用-創(chuàng)新互聯(lián)

今天寫了個(gè)示例代碼,就是使用Actionbar類實(shí)現(xiàn)頂部標(biāo)簽切換功能。如果所示。

十余年的峰峰礦網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整峰峰礦建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“峰峰礦網(wǎng)站設(shè)計(jì)”,“峰峰礦網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

 Android之Actionbar頂部標(biāo)簽的使用

使用最新的adt工具,創(chuàng)建項(xiàng)目的時(shí)候都會(huì)帶一個(gè)android-support-v7-appcompat的類庫項(xiàng)目,

這個(gè)libproject中有我們要用的ActionBar,可以適配2.1的Android系統(tǒng)。

廢話不多說,直接上代碼。

1、修改activity_main.xml,增加ViewPager。

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2、修改MainActivity中的代碼,讓其繼承ActionBarActivity

public class MainActivity extends ActionBarActivity implements TabListener {

3、創(chuàng)建TabsPagerAdapter繼承FragmentPagerAdapter

package com.example.tabswithswie.adatper;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.example.tabswithswie.fragments.AppFragment;
import com.example.tabswithswie.fragments.GamesFragment;
import com.example.tabswithswie.fragments.MoviesFragment;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int index) {
        switch (index) {
        case 0:
            return new AppFragment();
        case 1:
            return new GamesFragment();
        case 2:
            return new MoviesFragment();
     
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

4、創(chuàng)建AppFragment繼承android.support.v4.app.Fragment

package com.example.tabswithswie.fragments;

import com.example.tabswithswie.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AppFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_app, container, false);
    }
}

5、創(chuàng)建布局文件fragment_app.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#43ff00ff" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"
        android:text="這個(gè)是應(yīng)用界面"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

6、創(chuàng)建GamesFragment繼承android.support.v4.app.Fragment

package com.example.tabswithswie.fragments;

import com.example.tabswithswie.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class GamesFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_game, container, false);
    }
}

7、創(chuàng)建布局文件fragment_game.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="#9445f353">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"
        android:text="游戲"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

8、創(chuàng)建MoviesFragment繼承android.support.v4.app.Fragment

package com.example.tabswithswie.fragments;

import com.example.tabswithswie.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MoviesFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_movie, container, false);
    }
}

9、創(chuàng)建布局文件fragment_movie.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#34fef443" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"
        android:text="視頻"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

10、回到 MainActivity類,添加Tabs到ActionBar中,并處理點(diǎn)擊滑動(dòng)事件。完整代碼

package com.example.tabswithswie;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;

import com.example.tabswithswie.adatper.TabsPagerAdapter;

public class MainActivity extends ActionBarActivity implements TabListener {
    private ViewPager viewPager;
    private ActionBar actionBar; 
    private TabsPagerAdapter mTabsPagerAdapter;
    
    private String[] tabs ={"應(yīng)用","游戲","視頻"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取viewpager
        viewPager = (ViewPager) findViewById(R.id.pager);
        //實(shí)例化pageradapter
        mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(mTabsPagerAdapter);
        //獲取適配的actionbar
        actionBar = getSupportActionBar();
        //設(shè)置home按鈕不可點(diǎn)擊
        actionBar.setHomeButtonEnabled(false);
        //設(shè)置頂部導(dǎo)航的模式  -tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        //添加標(biāo)簽
        for(String tab:tabs)
        {
            actionBar.addTab(actionBar.newTab().setText(tab).setTabListener(this));
         }
        //設(shè)置ViewPager切換時(shí)候的監(jiān)聽事件
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            
            @Override
            public void onPageSelected(int position) {
                //頁面滑動(dòng),頂部標(biāo)簽跟著改變
                 actionBar.setSelectedNavigationItem(position);
            }
            
            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub
                
            }
        });
    }
     

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        //tab選中,切換viewpager
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        
    }

     
}

代碼就是這樣的,搞定收工了。示例代碼下載

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

當(dāng)前標(biāo)題:Android之Actionbar頂部標(biāo)簽的使用-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article0/dspcoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化網(wǎng)站排名、企業(yè)網(wǎng)站制作、ChatGPT、商城網(wǎng)站、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)