如何打开活动与动作条的对话 [英] How to open Activity as a dialog with ActionBar

查看:142
本文介绍了如何打开活动与动作条的对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开活动的对话框

 公共类MoveActivity延伸活动{
    私人的ListView列表;
    私人DocAdapter适配器;
    私人的ArrayList<字典<字符串,字符串>> mlist;
    DatabaseHelper帮手;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_move);
        名单=(ListView控件)findViewById(R.id.list);
        辅助= ApplicationClass.getDatabaseHelperInstance();
        helper.open(DatabaseHelper.readMode);
        mlist = helper.getDocList();
        helper.close();
        适配器=新DocAdapter(这一点,mlist);
        list.setAdapter(适配器);
    }

}
 

解决方案

有关对话框中使用行动起来吧,你必须在你的style.xml与父母为Theme.AppCompat.Light创建自定义主题。

 <样式名称=PopupTheme父=Theme.AppCompat.Light>
        <项目名称=机器人:窗框> @空< /项目>
        <项目名称=机器人:windowIsFloating>假< /项目>
        <项目名称=机器人:windowContentOverlay> @空< /项目>
        <项目名称=机器人:windowAnimationStyle> @android:款式/ Animation.Dialog< /项目>
        <项目名称=机器人:windowSoftInputMode> stateAlwaysHidden< /项目>
        <项目名称=机器人:windowActionModeOverlay>真< /项目>
        <项目名称=机器人:colorBackgroundCacheHint> @空< /项目>
        <项目名称=机器人:windowCloseOnTouchOutside>真< /项目>
        <项目名称=机器人:windowIsTranslucent>真< /项目>
    < /风格>
 

而在你的清单中添加此风格的活动标签:

 <活动
            机器人:MyActivityNAME =
            机器人:configChanges =定位| keyboardHidden |语言环境
            机器人:screenOrientation =画像
            机器人:主题=@风格/ PopupTheme>
 

和前 setConytentView(layoutID)最后添加此code。在您的活动;

  @覆盖
    保护无效的onCreate(包savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
        this.getWindow()。setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
            WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        的LayoutParams PARAMS = this.getWindow()的getAttributes()。
        params.alpha = 1.0F;
        params.dimAmount = 0.5F;
        。this.getWindow()setAttributes((android.view.WindowManager.LayoutParams)PARAMS);

        //该设置窗口的大小,同时抛向四周ActionBarView的IllegalStateException异常工作
        this.getWindow()的setLayout(600900)。

        的setContentView(R.layout.dialog_move);
}
 

I want to open activity as dialog

public class MoveActivity extends Activity {
    private ListView list;
    private DocAdapter adapter;
    private ArrayList<Dictionary<String, String>> mlist;
    DatabaseHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move);
        list = (ListView) findViewById(R.id.list);
        helper = ApplicationClass.getDatabaseHelperInstance();
        helper.open(DatabaseHelper.readMode);
        mlist = helper.getDocList();
        helper.close();
        adapter = new DocAdapter(this, mlist);
        list.setAdapter(adapter);
    }

}

解决方案

For using action bar in Dialog, you have to create a custom theme in your style.xml with parent as "Theme.AppCompat.Light".

<style name="PopupTheme" parent="Theme.AppCompat.Light">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
        <item name="android:windowActionModeOverlay">true</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>

And add this style in your manifest with activity tag:

<activity
            android:name=".MyActivity"
            android:configChanges="orientation|keyboardHidden|locale"
            android:screenOrientation="portrait"
            android:theme="@style/PopupTheme" >

and last add this code in your activity before setConytentView(layoutID);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
            WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        LayoutParams params = this.getWindow().getAttributes(); 
        params.alpha = 1.0f;
        params.dimAmount = 0.5f;
        this.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 

        // This sets the window size, while working around the IllegalStateException thrown by ActionBarView
        this.getWindow().setLayout(600,900);

        setContentView(R.layout.dialog_move);
}

这篇关于如何打开活动与动作条的对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆