上下文动作条与DialogFragment [英] Contextual Actionbar with DialogFragment

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

问题描述

我一直在试图实现一个上下文操作栏与对话片段。 类似的下载窗口小部件的机器人。

I've been trying to implement a contextual action bar along with a dialog fragment. Similar to the downloads widget in android.

我试着设置安卓windowActionModeOverlay 在主题为真。

I've tried to set android:windowActionModeOverlay to be true in the theme.

但它似乎没有工作。有没有什么方法可以让我实现它?

But it doesnt seem to work. Is there any way I can achieve it??

推荐答案

下载窗口,你在你的屏幕截图实际上是一个活动使用 @android:款式/ Theme.Holo.Dialog 主题,这使得它看起来像一个对话框。为了达到相同的外观的下载窗口,你的活动只需要使用相同的主题。

The downloads window that you have in your screenshot is actually an Activity using the @android:style/Theme.Holo.Dialog theme which makes it look like a dialog. To achieve the same look as the downloads window, your Activity need only use the same theme.

您可以设置这个主题在你的清单如下所示:

You can set this theme in your manifest like so:

<activity android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo.Dialog" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>


示例执行不包括字符串,可绘制的资源。

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mceley.dialog.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Holo.Dialog" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java:

MainActivity.java:

package com.mceley.dialog.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        findViewById(R.id.context_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        ExampleMode mode = new ExampleMode();
        startActionMode(mode);
    }

    public class ExampleMode implements ActionMode.Callback {

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.main_menu, menu);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
    }
}

main_layout.xml:

main_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <Button android:id="@+id/context_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_context_bar" />

</LinearLayout>

main_menu.xml:

main_menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_settings"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
</menu>

结果:

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

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