10

Tôi muốn thêm đoạn vào hộp thoại (có thể là Hộp thoại hoặc Hộp thoại thông thường). Làm thế nào để làm điều đó?Thêm đoạn vào hộp thoại

Dưới đây là DialogFragment tôi:

public class MyDialogFragment extends DialogFragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     MyDialogFragment2 dialog = new MyDialogFragment2(); 
     View v = inflater.inflate(R.layout.news_articles, container, false); 
     getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, dialog).commit(); 
     return v; 
    } 

} 

Dưới đây là news_article.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

Đây là hoạt động chính của tôi:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      MyDialogFragment dialog = new MyDialogFragment(); 
      dialog.show(getSupportFragmentManager(), "asdf"); 
     } 
    }); 
} 

Nhưng khi tôi thử nó tôi nhận được:

No view found for id 0x7f070002 for fragment MyDialogFragment2 

Tôi nghĩ rằng đó là bởi vì FragmentManager của Activity không phải là một trong những tôi nên thêm vào, nhưng tôi không thể tìm thấy một trong các DialogFragment, ở đâu? .

+2

'getChildFragmentManager(). StartsTransaction() ....' – Luksprog

+0

Cảm ơn, nhưng điều này chỉ hoạt động từ API 17, phải không? – Kalisky

+1

Với các mảnh gốc có khi chúng được giới thiệu từ 4.2. Nhưng bạn luôn có tùy chọn của các mảnh từ gói tương thích hỗ trợ làm việc với cùng phương thức 'getChildFragmentManager()'. – Luksprog

Trả lời

7

Layout cho Dialog - R.layout.view_with_plus

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.util.me.TestActivity" 
    > 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Details"/> 
    <fragment 
     android:layout_toRightOf="@+id/text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/email" 
     class="com.util.me.test.PlusOneFragment" 
     android:layout_centerHorizontal="true"/> 

</RelativeLayout> 

Làm thế nào để hiển thị Dialog

public void showDialog(View vIew){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    View view = this.getLayoutInflater().inflate(R.layout.view_with_plus, null); 
    builder.setView(view) 
      .setPositiveButton("OK", null) 
      .setNegativeButton("Cancel", null); 

    AlertDialog dialog = builder.create(); 
    dialog.show(); 
} 

Phân đoạn trong lớp = "com.util.me.test.PlusOneFragment" chỉ là một PlusOneFragment được tạo bởi Android Studio.