Contextual Actionbar with DialogFragment

蓝咒 提交于 2019-12-10 14:15:23

问题


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

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


回答1:


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>

Example implementation excluding string and drawable resources.

Manifest:

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

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:

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

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

Result:




回答2:


I don't think you can add ActionBar in DialogFragment.

But We can Try that using Activity As Dialog.

I have tried this with ActionBarSherlock and I have added R.style.Sherlock___Theme_Dialog as my Activity's theme but it looks like this:

After doing Above thing i understud, we can't Add ActionBar in Dialog or DialogFragment.



来源:https://stackoverflow.com/questions/15862579/contextual-actionbar-with-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!