5

Tôi đã cố gắng triển khai thanh tác vụ theo ngữ cảnh cùng với một đoạn hộp thoại. Tương tự như tiện ích tải xuống trong Android.Thanh tác vụ theo bối cảnh với DialogFragment

enter image description here

Tôi đã cố gắng để thiết lập android:windowActionModeOverlay đến mức khó tin trong chủ đề.

Nhưng dường như nó không hoạt động. Có cách nào tôi có thể đạt được nó?

Trả lời

4

Cửa sổ tải xuống mà bạn có trong ảnh chụp màn hình thực sự là một Activity sử dụng chủ đề @android:style/Theme.Holo.Dialog làm cho giao diện trông giống như một hộp thoại. Để đạt được cùng một giao diện như cửa sổ tải xuống, Activity của bạn chỉ cần sử dụng cùng một chủ đề.

Bạn có thể đặt chủ đề này trong biểu hiện của bạn như sau:

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

Ví dụ thực hiện trừ chuỗi và các nguồn lực có thể vẽ được.

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> 

Kết quả:

Dialog Example App

+0

tôi đã đi qua này khi tìm kiếm. Tôi muốn biết nếu có một cách để làm điều đó mà không tạo ra một hoạt động cho mảnh vỡ. Anyways .. Cảm ơn –

0

Tôi không nghĩ bạn có thể thêm ActionBar vào DialogFragment.

Nhưng chúng tôi có thể thử sử dụng hoạt động như hộp thoại.

Tôi đã thử điều này với ActionBarSherlock và tôi đã thêm R.style.Sherlock___Theme_Dialog như chủ đề Hoạt động của tôi, nhưng nó trông như thế này:

enter image description hereenter image description hereenter image description here

Sau khi thực hiện điều trên tôi understud, chúng ta không thể Thêm ActionBar trong Dialog hoặc DialogFragment.

Các vấn đề liên quan