如何将我的活动转为对话框

时间:2014-02-05 15:05:36

标签: android

如何将当前活动设置为对话框?我非常感谢任何帮助。谢谢。 ICS及以上。

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);





           pager = (ViewPager)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);



    }


    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

    }



}

XML

<?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>

1 个答案:

答案 0 :(得分:0)

您可以创建一个新的Activity,然后可以使它像对话一样。如果您使用4.0之前的设备,则必须执行以下操作:

创建文件夹res / values-v11(这适用于API 11+),然后添加名为styles.xml的文件。在此文件中键入以下内容:

<resources>
    <style name="my_activity_theme" parent="android:Theme.Holo.Light"/>
</resources>

然后在位于in / res / values文件夹中的styles.xml中添加以下行:

<resources>
    <style name="my_activity_theme" parent="android:Theme.Light"/>
</resources>

然后,将其应用于AndroidManifest.xml中的应用程序:

<activity
    ...
    <android:theme="@style/my_activity_theme"/>

</activity

这样您就可以进行浮动活动。我没有测试过,但我认为值得尝试。

希望这就是你要找的东西