如何在Android中的TabView上方添加自定义操作栏?
我的一个类中有一个选项卡视图。现在我想在选项卡视图上方放置一个自定义操作栏。我用ActionBarSherlock来做这个。所以现在我的问题是,为了使用Actionbar,我应该扩展SherlockActivity。但是我需要扩展TabActivity来实现Tabs。我如何解决这个问题?谁来帮忙 这是我的课如何在Android中的TabView上方添加自定义操作栏?,android,tabs,actionbarsherlock,Android,Tabs,Actionbarsherlock,我的一个类中有一个选项卡视图。现在我想在选项卡视图上方放置一个自定义操作栏。我用ActionBarSherlock来做这个。所以现在我的问题是,为了使用Actionbar,我应该扩展SherlockActivity。但是我需要扩展TabActivity来实现Tabs。我如何解决这个问题?谁来帮忙 这是我的课 public class ActivityRestaurantMenuListTab extends TabActivity { TabHost tabHost;
public class ActivityRestaurantMenuListTab extends TabActivity {
TabHost tabHost;
HorizontalScrollView mHorizontalScrollView;
int tabnum;
String RestaurantName="Restaurant 1";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.restaurant_menu_list_tab);
ActionBar actionBar = getSupportActionBar();
View actionBarView = getLayoutInflater().inflate(
R.layout.custom_actionbar_restaurant, null);
ImageView back = (ImageView) actionBarView.findViewById(R.id.btn_back);
ImageView table = (ImageView) findViewById(R.id.btn_table);
ImageView basket = (ImageView) findViewById(R.id.btn_basket);
TextView tableno = (TextView) findViewById(R.id.txt_table_no);
TextView basketitems = (TextView) findViewById(R.id.txt_basket_items);
TextView actionBarTitle = (TextView) actionBarView
.findViewById(R.id.txt_title);
actionBarTitle.setText(RestaurantName);
basket.setVisibility(View.VISIBLE);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
Intent intent = getIntent();
tabnum = intent.getIntExtra("category", 0);
Toast.makeText(getApplicationContext(), "Selected tab is " + tabnum,
Toast.LENGTH_SHORT).show();
tabHost = (TabHost) findViewById(android.R.id.tabhost);
mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.hs);
TabWidget widget = tabHost.getTabWidget();
TabSpec tab1 = tabHost.newTabSpec("Item 1");
TabSpec tab2 = tabHost.newTabSpec("Item 2");
TabSpec tab3 = tabHost.newTabSpec("Item 3");
TabSpec tab4 = tabHost.newTabSpec("Item 4");
tab1.setIndicator("Item 1");
tab1.setContent(new Intent(this, Activity.class));
tab2.setIndicator("Item 2");
tab2.setContent(new Intent(this, Activity.class));
tab3.setIndicator("Item 3");
tab3.setContent(new Intent(this, Activity.class));
tab4.setIndicator("Item 4");
tab4.setContent(new Intent(this, Activity.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
float scale = getResources().getDisplayMetrics().density;
final double tabWidth = (int) (150 * scale + 0.5f);
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
tabHost.getTabWidget().getChildTabViewAt(i).getLayoutParams().width = (int) tabWidth;
}
final double screenWidth = getWindowManager().getDefaultDisplay()
.getWidth();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
int nrOfShownCompleteTabs = ((int) (Math.floor(screenWidth
/ tabWidth) - 1) / 2) * 2;
int remainingSpace = (int) ((screenWidth - tabWidth - (tabWidth * nrOfShownCompleteTabs)) / 2);
int a = (int) (tabHost.getCurrentTab() * tabWidth);
int b = (int) ((int) (nrOfShownCompleteTabs / 2) * tabWidth);
int offset = (a - b) - remainingSpace;
mHorizontalScrollView.scrollTo(offset, 0);
}
});
for (int i = 0; i < widget.getChildCount(); i++) {
View v = widget.getChildAt(i);
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setAllCaps(false);
tv.setTextSize(15);
if (tv == null) {
continue;
}
tabHost.setCurrentTab(tabnum);
v.setBackgroundResource(R.drawable.tabselector);
tabHost.getTabWidget().setDividerDrawable(null);
}
}
}
公共类活动RestaurantMenuListTab扩展了TabActivity{
TabHost-TabHost;
水平滚动视图M水平滚动视图;
int-tabnum;
字符串RestaurantName=“Restaurant 1”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、餐厅菜单列表选项卡);
ActionBar ActionBar=getSupportActionBar();
View actionBarView=GetLayoutFlater()。充气(
R.layout.custom_actionbar_restaurant,空);
ImageView back=(ImageView)actionBarView.findViewById(R.id.btn\u back);
ImageView表格=(ImageView)findViewById(R.id.btn_表格);
ImageView篮=(ImageView)findViewById(R.id.btn_篮);
TextView表号=(TextView)findViewById(R.id.txt\u表号);
TextView basketitems=(TextView)findViewById(R.id.txt_basket_items);
TextView actionBarTitle=(TextView)actionBarView
.findviewbyd(R.id.txt_title);
actionBarTitle.setText(餐厅名称);
basket.setVisibility(View.VISIBLE);
back.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
完成();
}
});
actionBar.setCustomView(actionBarView);
actionBar.setDisplayOptions(actionBar.DISPLAY\u SHOW\u CUSTOM);
Intent=getIntent();
tabnum=intent.getIntExtra(“类别”,0);
Toast.makeText(getApplicationContext(),“所选选项卡为”+tabnum,
吐司。长度(短)。show();
tabHost=(tabHost)findviewbyd(android.R.id.tabHost);
mHorizontalScrollView=(HorizontalScrollView)findViewById(R.id.hs);
TabWidget widget=tabHost.getTabWidget();
TabSpec tab1=tabHost.newTabSpec(“第1项”);
TabSpec tab2=tabHost.newTabSpec(“第2项”);
TabSpec tab3=tabHost.newTabSpec(“第3项”);
TabSpec tab4=tabHost.newTabSpec(“第4项”);
表1.设置指示器(“第1项”);
tab1.setContent(新意图(this,Activity.class));
表2.设置指示器(“第2项”);
tab2.setContent(新意图(this,Activity.class));
表3.设置指示器(“第3项”);
tab3.setContent(新意图(this,Activity.class));
表4.设置指示器(“第4项”);
tab4.setContent(新意图(this,Activity.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
tabHost.addTab(tab4);
浮动比例=getResources().getDisplayMetrics().density;
最终双制表符宽度=(整数)(150*刻度+0.5f);
对于(int i=0;i
尝试在xml文件的选项卡视图上方添加一个宽度匹配父级和高度环绕内容的相对布局或线性布局。我们可以提供背景颜色,我们可以在顶部dummyactionbar布局中放置图标,避免扩展Sherlock活动
示例xml文件可以是
<RelativeLayout
android:layout_height="@dimen/titlebarsize"
android:layout_width="match_parent"
android:background="@color/red"
android:layout_alignParentTop="true"
android:id="@+id/topbar"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/title"
android:text="@string/steptwotitle"
style="@style/actionbarphonetextviewstyle"
/>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/back"
android:id="@+id/back"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:background="@null"
android:visibility="gone"
/>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/sidemenutest"
android:id="@+id/sidemenu"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:background="@null"
/>
</RelativeLayout>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@+id/topbar"
android:layout_centerHorizontal="true"
>
在活动标记内的清单中添加
android:theme=“@android:style/theme.NoTitleBar.Fullscreen”操作栏中的按钮点击情况如何?您可以像平常的按钮点击一样使用。
protected void onCreate(Bundle savedInstanceState) {
back=(ImageButton)findViewById(R.id.back);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in=new Intent(CommentsPage.this,Playmyfile.class);
startActivity(in);
}
});
}