2015-06-24 21 views
5

Tôi đang cố gắng để thêm một hộp thoại để ứng dụng Android của tôi mà là toàn màn hình trên các thiết bị nhỏ (điện thoại di động ví dụ) nhưng một hộp thoại tiêu chuẩn trên các thiết bị lớn (ví dụ viên nén). Điều này theo logic được trình bày trong số material design specification.thoại toàn màn hình Android xuất hiện minh bạch và ở vị trí sai

Sử dụng official android dialog guide, sử dụng một DialogFragment, tôi kết thúc với một hộp thoại trong suốt chồng lên thanh hành động:

enter image description here

Dưới đây là mã nguồn.

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/toolbar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <!-- Use ThemeOverlay to make the toolbar text white --> 
     <android.support.design.widget.AppBarLayout 
      android:id="@+id/abl_top" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:fitsSystemWindows="true" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:layout_scrollFlags="scroll|enterAlways"/> 

     </android.support.design.widget.AppBarLayout> 
    </android.support.design.widget.CoordinatorLayout> 

    <FrameLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="10dp" 
     android:src="@drawable/ic_add" 
     android:clickable="true"/> 

</android.support.design.widget.CoordinatorLayout> 

MainActivity.java

package com.example.fsdialog; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 

public class MainActivity extends AppCompatActivity 
{ 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Setup AppBar 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     if (toolbar != null) { 
      setSupportActionBar(toolbar); 
     } 

     final ActionBar ab = getSupportActionBar(); 
     ab.setHomeAsUpIndicator(R.drawable.ic_menu); 
     ab.setDisplayHomeAsUpEnabled(true); 

     // FAB 
     FloatingActionButton fab = (FloatingActionButton) findViewById(
       R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       FragmentManager fm = getSupportFragmentManager(); 
       MyDialogFragment dialog = new MyDialogFragment(); 

       if (false) { 
        // The device is using a large layout, so show the fragment 
        // as a dialog 
        dialog.show(fm, "MyDialogFragment"); 
       } else { 
        // The device is smaller, so show the fragment fullscreen 
        FragmentTransaction tx = fm.beginTransaction(); 
        tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 

        // To make it fullscreen, use the 'content' root view as 
        // the container for the fragment, which is always the root 
        // view for the activity 
        tx.add(R.id.content, dialog) 
         .addToBackStack(null).commit(); 
       } 

      } 
     }); 
    } 

} 

my_dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:rowCount="2" 
    android:columnCount="2"> 

    <TextView 
     android:id="@+id/text_date1" 
     android:layout_row="0" 
     android:layout_column="0" 
     android:text="17/06/2015" 
     style="@android:style/Widget.Holo.Spinner"/> 
    <TextView 
     android:id="@+id/text_time1" 
     android:layout_row="0" 
     android:layout_column="1" 
     android:text="09:35" 
     style="@android:style/Widget.Holo.Spinner"/> 
    <TextView 
     android:id="@+id/text_date2" 
     android:layout_row="1" 
     android:layout_column="0" 
     android:text="17/06/2015" 
     style="@android:style/Widget.Holo.Spinner"/> 
    <TextView 
     android:id="@+id/text_time2" 
     android:layout_row="1" 
     android:layout_column="1" 
     android:text="11:00" 
     style="@android:style/Widget.Holo.Spinner"/> 

</GridLayout> 

MyDialogFragment.java

package com.example.fsdialog; 

import android.app.Dialog; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.LayoutInflater; 
import android.view.Window; 

public class MyDialogFragment 
    extends DialogFragment 
{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.my_dialog, container, false); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     Dialog dialog = super.onCreateDialog(savedInstanceState); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     return dialog; 
    } 
} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.example.fsdialog" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:label="@string/app_name" 
       android:icon="@drawable/ic_launcher"> 
     <activity android:name="MainActivity" 
        android:label="@string/app_name" 
        android:theme="@style/Theme.AppCompat.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 

CẬP NHẬT 1 (24-06-2015)

bây giờ tôi đang sử dụng Theme.AppCompat.NoActionBar theme android và có a FrameLayout trong hoạt động chính. Tôi vẫn nhận được cùng một vấn đề (hình ảnh được cập nhật).

  • Tôi có giả định rằng chủ đề tích hợp sẽ chứa màu nền cho hộp thoại không? Bạn biết đấy, một mặc định lành mạnh? Tôi biết chắc chắn rằng nếu tôi chỉ hiển thị một hộp thoại theo cách thông thường, nó sẽ có màu nền.
  • Tôi giả định CoordinatorLayout tôi đã thêm khiến hộp thoại phủ lên thanh tác vụ mặc dù Tôi đang sử dụng FrameLayout được nhúng. Tại sao vậy? Tôi cần CoordinatorLayout để FloatingActionButton ở đúng vị trí.
+0

giải pháp dễ nhất là để thiết lập thuộc tính nền tảng của bố cục DialogFragment của bạn ('my_dialog.xml') để bất cứ điều gì bạn muốn nó được. Cách chính xác để thực hiện nó là nhìn vào chủ đề bạn đang sử dụng và thiết lập các thuộc tính chính xác về chủ đề cho hành vi/phong cách mong muốn của bạn (Tôi không biết những gì đang ở trên đầu của tôi). – kha

+0

Bạn đang thêm tính năng hộp thoại vào bố cục gốc (bố cục hoạt động) ...đó là lý do tại sao nó được đặt ở vị trí của nó. Sử dụng một số FrameLayout bên trong bố trí gốc và vị trí nó, nơi bạn muốn có hộp thoại của bạn trên máy tính bảng (tức là centerinparent của relativelayout). Bạn không có bất kỳ nền nào được đặt cho bố cục hộp thoại của mình. Hộp thoại là minh bạch vì điều đó. – cutoff

+0

Tôi đã cập nhật câu hỏi của mình để bao gồm mã mới với FrameLayout và sử dụng chủ đề tích hợp sẵn. Vấn đề vẫn còn. – Jon

Trả lời

1

@cutoff là đúng. Bản cập nhật của tôi không thành công do bố cục gốc là CoordinatorLayout thay vì LinearLayout hoặc thứ gì đó không kém phần tốt. Ví dụ dưới đây sử dụng một DrawerLayout:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.design.widget.CoordinatorLayout 
      android:id="@+id/toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <!-- Use ThemeOverlay to make the toolbar and tablayout text 
       white --> 
      <android.support.design.widget.AppBarLayout 
       android:id="@+id/abl_top" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:fitsSystemWindows="true" 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
        app:layout_scrollFlags="scroll|enterAlways"/> 

      </android.support.design.widget.AppBarLayout> 
     </android.support.design.widget.CoordinatorLayout> 

     <FrameLayout 
      android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header" 
     app:menu="@menu/nav_view"/> 

</android.support.v4.widget.DrawerLayout> 

Thông tin chi tiết về giải pháp này có thể được tìm thấy trong this question, bao gồm cả lý do tại sao một DialogFragment là đủ cho bất cứ điều gì khác hơn là toàn màn hình hoàn toàn.

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