[英]Custom Action bar in Android?
我已經開發了一個Android應用程序,其中有4個TabHost片段。
我知道如何在MainActivity中自定義ActionBar
。
但是問題是如何根據不同Fragment
的4個不同的TabHost定制我的ActionBar
?
這是我的tabHost代碼-
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/** Creating ANDROID Tab */
Tab tab = actionBar.newTab()
//.setText("Android")
.setTabListener(new CustomTabListener<PlayFragment01>(this, "play",
PlayFragment01.class))
.setIcon(R.drawable.playtabhosticon);
actionBar.addTab(tab);
/** Creating APPLE Tab */
Tab tab1 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<VenueFragment01>(this, "venu",
VenueFragment01.class))
.setIcon(R.drawable.venutabhosticon);
actionBar.addTab(tab1);
/** Creating APPLE Tab */
Tab tab3 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<SocialFragment01>(this, "social", SocialFragment01.class))
.setIcon(R.drawable.socialtabhosticon);
actionBar.addTab(tab3);
/** Creating APPLE Tab */
Tab tab4 = actionBar.newTab()
//.setText("Apple")
.setTabListener(new CustomTabListener<ActivityFragment01>(this, "activity",
ActivityFragment01.class))
.setIcon(R.drawable.actionbartabhosticon);
actionBar.addTab(tab4);
}
}
這是我的第一個片段代碼-
package in.wptrafficanalyzer.actionbarnavtab;
/** This is a listfragment class */
public class PlayFragment01 extends Fragment {
/** An array of items to display in ArrayList */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
/** Creating array adapter to set data in listview */
View rootView = inflater.inflate(R.layout.fragment_play, container, false);
//new DownloadJSON().execute();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#0077d1")));
ActionBar mActionBar = getActivity().getActionBar();
getActivity().getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar2, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
return rootView;
}
}
您提出的問題不是很清楚,但是您可以使用getActivity().getActionBar()
從Fragment中獲取ActionBar實例。
現在不推薦使用ActionBar.setNavigationMode()
和TabHost
。
API-21中引入的新ToolBar
功能更強大。
但是,由於您需要一種解決方案,因此這里有一個來自Google SlidingTabLayout的簡單解決方案:這就像一個PagerTabStrip
,您可以輕松自定義它並且效果很好。
當然,它可以與ViewPager
和Fragments
使用。
以我的觀點,您應該閱讀此內容,以便獲得創建片段的基本思路。
http://developer.android.com/guide/components/fragments.html
試試這個示例代碼。.這個示例代碼包括3個標簽,首先嘗試了解此代碼,然后您可以使用4個標簽制作自己的應用
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
也嘗試修改您的問題,目前尚不清楚,也可以張貼您的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.