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? .
'getChildFragmentManager(). StartsTransaction() ....' – Luksprog
Cảm ơn, nhưng điều này chỉ hoạt động từ API 17, phải không? – Kalisky
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