如何设置全部的activity使用一个actionbar
1个回答
展开全部
<item
android:id="@+id/action_clock"
android:orderInCategory="100"
android:showAsAction="always"
android:title="@string/action_settings"
android:actionLayout="@layout/clock"/>
</menu>
2、在写一个时钟的布局文件,clock.xml:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3、在定义一个布局文件用来存放Fragment的布局,列出其中一个,fragment_1.xml:
[html] view plain copy
<?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" >
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="@drawable/xianjian01" >
</ImageView>
</LinearLayout>
4、定义一个类实现ActionBar.TabListener的接口,在这个实现中,每个Tab选项标签都使用了它自己的监听器,MyTabListener.Java:
[java] view plain copy
package com.yangyu.myactionbar02;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class MyTabListener<T extends Fragment> implements TabListener {
private Fragment fragment;
private final Activity mActivity;
private final Class<T> mClass;
public MyTabListener(Activity activity, Class<T> clz){
mActivity = activity;
mClass = clz;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(fragment == null){
fragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, fragment, null);
}
ft.attach(fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.detach(fragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
android:id="@+id/action_clock"
android:orderInCategory="100"
android:showAsAction="always"
android:title="@string/action_settings"
android:actionLayout="@layout/clock"/>
</menu>
2、在写一个时钟的布局文件,clock.xml:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3、在定义一个布局文件用来存放Fragment的布局,列出其中一个,fragment_1.xml:
[html] view plain copy
<?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" >
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="@drawable/xianjian01" >
</ImageView>
</LinearLayout>
4、定义一个类实现ActionBar.TabListener的接口,在这个实现中,每个Tab选项标签都使用了它自己的监听器,MyTabListener.Java:
[java] view plain copy
package com.yangyu.myactionbar02;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
public class MyTabListener<T extends Fragment> implements TabListener {
private Fragment fragment;
private final Activity mActivity;
private final Class<T> mClass;
public MyTabListener(Activity activity, Class<T> clz){
mActivity = activity;
mClass = clz;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if(fragment == null){
fragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, fragment, null);
}
ft.attach(fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (fragment != null) {
ft.detach(fragment);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询