如何打开Activity作为ActionBar的对话框

时间:2013-10-31 07:25:15

标签: android

我想以对话框

打开活动
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);
    }

}

3 个答案:

答案 0 :(得分:12)

要在Dialog中使用操作栏,您必须在style.xml中创建一个自定义主题,其父级为“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>

并在您的清单中使用活动代码添加此样式:

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

并最后在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);
}

答案 1 :(得分:0)

AndroidManifest.xml

中定义的对话框启动活动
<activity android:theme="@android:style/Theme.Dialog" />

答案 2 :(得分:-2)

清单文件中的

将活动的主题定义为对话框。

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