[英]Custom Action bar in Android?
I have developed an Android application which has 4 TabHost's which are in fragments. 我已经开发了一个Android应用程序,其中有4个TabHost片段。
I Know how to customize the ActionBar
in MainActivity. 我知道如何在MainActivity中自定义ActionBar
。
But problem is how can I customize my ActionBar
according to the 4 different TabHost's in different Fragment
s? 但是问题是如何根据不同Fragment
的4个不同的TabHost定制我的ActionBar
?
Here is my tabHost code - 这是我的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);
}
}
Here is my first fragment code - 这是我的第一个片段代码-
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()
and TabHost
is now deprecated. 现在不推荐使用ActionBar.setNavigationMode()
和TabHost
。
The new ToolBar
introduced in API-21 is more powerfull. API-21中引入的新ToolBar
功能更强大。
But since you asked for a solution, here is a simple one from Google SlidingTabLayout : This is like a PagerTabStrip
that you can easily customize and works like great. 但是,由于您需要一种解决方案,因此这里有一个来自Google SlidingTabLayout的简单解决方案:这就像一个PagerTabStrip
,您可以轻松自定义它并且效果很好。
It works with ViewPager
and Fragments
of course. 当然,它可以与ViewPager
和Fragments
使用。
In my point of view ,you should read this ,so that you can get the basic idea for creating a fragment. 以我的观点,您应该阅读此内容,以便获得创建片段的基本思路。
http://developer.android.com/guide/components/fragments.html http://developer.android.com/guide/components/fragments.html
Try this sample code..this sample code includes 3 tab ,first try to understand this code ,then you can make your own app with 4 tabs 试试这个示例代码。.这个示例代码包括3个标签,首先尝试了解此代码,然后您可以使用4个标签制作自己的应用
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/ http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
Also try to modify your question ,It is unclear and also it will be good that you post your errors 也尝试修改您的问题,目前尚不清楚,也可以张贴您的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.