編輯:關於Android編程
下拉式導航:
final ActionBar actionBar = getSupportActionBar();
//設置ActionBar是否顯示標題
actionBar.setDisplayShowTitleEnabled(false);
//設置導航模式,使用List導航
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// 為導航設置列表項數據源和監聽器
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter(//為導航設置列表項
actionBar.getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[] {
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
}),
this);//這個this為導航設置監聽器ActionBar.OnNavigationListener,如下所示
//當導航被選中時激發該方法
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
return true;
}
//內部類,根據所選列表id動態創建並返回的Fragment類
public static class PlaceholderFragment extends Fragment {} //設置ActionBar 的Tabs導航
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//FragmentPaperAdapter對象(下面附上),這個適配器根據選擇返回對應的Fragment
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
//ViewPaper是Fragment的容器,可以同時管理多個Fragment,並允許多個Fragment切換時提供動畫效果,需要為它設置適配器FragmentPagerAdapter
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// 為ViewPaper設置監聽器,當ViewPaper顯示的Fragment發生改變時激發該方法
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// 遍歷paperAdapter對象所包含的全部Fragment,每個Fragment對應創建一個Tab標簽,並設置ActionBar的事件監聽接口對象TabListener
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this)
);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
獲取第position位置的Fragment
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
//該方法的返回值i表明該Adapter總共包括多少個Fragment
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
//該方法的返回值決定每個Fragment的標題
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}使用Tab標簽頁的一般步驟
首先要設計所有的分頁的界面布局
Activity繼承TabActivity
調用TabActivity的getTabHost()方法獲得TabHost對象
通過TabHost創建Tab
TabHost:標簽控件核心類,標簽的集合
TabHost.TabSpec:標簽對象,可以裝載View視圖。如一個控件或布局
代碼說明:
//聲明TabHost,然後用LayoutInflater過濾出布局來,給TabHost加上含有Tab頁面的FrameLayout
TabHost myTabhost=this.getTabHost();
//從TabActivity上面獲取放置Tab的TabHost
LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);
//from(this)從這個TabActivity獲取LayoutInflater
//R.layout.main 存放Tab布局
//通過TabHost獲得存放Tab標簽頁內容的FrameLayout
//是否將inflate 拴系到根布局元素上
在TabHost創建一個標簽,然後設置一下標題/圖標/標簽頁布局
myTabhost.addTab(myTabhost.newTabSpec("TT")// 造一個新標簽TT
.setIndicator("KK",getResources().getDrawable(R.drawable.ajjc))// 設置一下顯示的標題為KK,設置一下標簽圖標為ajjc
.setContent(R.id.widget_layout_red)); //設置一下該標簽頁的布局內容為R.id.widget_layout_red,這是FrameLayout中的一個子Layout //extends Fragment實現對應導航的回調方法public void onNavigationDrawerItemSelected(int position)
implements NavigationDrawerFragment.NavigationDrawerCallbacks
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
//返回對應的Fragment對象
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
//該方法的返回值決定每個Fragment的標題
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
//設置導航打開時的導航文字顯示
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
android canvas常用的方法解析(一)
我們知道要想繪制一些特別的效果的話,離不開Paint和Canvas,Paint是你所畫圖形的一些基本屬性,按照面向對象的思想,你要把一個圓畫在畫布上,那麼是有畫筆和畫布,
Android實現繞球心旋轉的引導頁效果
現在很多APP都會出現Android實現繞球心旋轉的引導頁效果,一個類似小車一直在往前開的旋轉式動畫效果。先看一下預覽效果:嗯,整體效果還算理想,基本實現了頁面繞屏幕底部
Cocos移植到Android的一些問題-中文亂碼問題
Android平台版本和設備碎片化很嚴重,因此從Win32平台移植到Android平台會有很多問題,下面是我們歸納的從Win32平台移植到Android平台遇到的一些問題
Android 帶進度條的WebView 示例代碼
前言 如果不使用系統自帶的TitleBar(即Activity被設置@android:style/Theme.NoTitleBar),那就需要自己來寫進度條了