我尝试使用标签栏/操作栏弹出窗口,但我在对话框后面有对话框和标签栏。如何在对话框中显示标签栏?我真的很感激任何帮助。谢谢你。
public class MainActivity extends Activity implements TabListener {
final String[] page_titles = new String[]{"Home", "contact", "Apps"};
//this will go the description TextView
final String[] desc = new String[]{
"This is the homepage ",
"contact",
"apps"
};
ViewPager pager;
ActionBar bar;
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_main);
pager = (ViewPager)dialog. findViewById(R.id.pager);
bar = getActionBar();
myAdaptertest1 mAdapter = new myAdaptertest1(this);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setHomeButtonEnabled(false);
pager.setAdapter(mAdapter);
pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
bar.setSelectedNavigationItem(position);
}
});
Tab hometab = bar.newTab();
hometab.setText("Home");
hometab.setTabListener(this);
bar.addTab(hometab);
Tab cameratab = bar.newTab();
cameratab.setText("contact");
cameratab.setTabListener(this);
bar.addTab(cameratab);
Tab exploretab = bar.newTab();
exploretab.setText("apps");
exploretab.setTabListener(this);
bar.addTab(exploretab);
bar.show();
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);
dialog.show();
}
private class myAdaptertest1 extends PagerAdapter{
Context context;
public myAdaptertest1(Context c){
this.context = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return page_titles.length;
}
@Override
public boolean isViewFromObject(View v, Object o) {
// TODO Auto-generated method stub
return v.equals(o);
}
public Object instantiateItem(View pager, int position) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.page, null, false);
TextView title = (TextView)vi.findViewById(R.id.tvTitle);
TextView description = (TextView) vi.findViewById(R.id.tvdesc);
title.setText(page_titles[position]);
description.setText(desc[position]);
//This is very important
( (ViewPager) pager ).addView( vi, 0 );
return vi;
}
@Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((View) view);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public static void showAsPopup(Activity activity) {
//To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest
activity.requestWindowFeature(Window.FEATURE_ACTION_BAR);
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
LayoutParams params = activity.getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
params.width = 850; //fixed width
params.alpha = 1.0f;
params.dimAmount = 0.5f;
activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pager"/>
</LinearLayout>