web-dev-qa-db-ja.com

フラグメントの作成におけるConstraintLayoutの使用

ConstraintLayoutを使用してフラグメントレイアウトを作成できるかどうか疑問に思っています!?それは適切なアプローチですか?またはフラグメントを作成するための標準的なルート要素はありますか?

12
mhd.saboori

ConstraintLayoutを使用してFragmentを含めることができます。 Fragmentのベースビューにすることもできます。ただし、FrameLayoutのような複雑なレイアウトを使用する理由はほとんどないため、通常はConstraintLayoutをホルダーとして使用します。

ご覧のとおり、FragmentTransaction.add()containerViewIdViewに制限を設けていません。

_/**
 * Add a fragment to the activity state.  This fragment may optionally
 * also have its view (if {@link Fragment#onCreateView Fragment.onCreateView}
 * returns non-null) inserted into a container view of the activity.
 *
 * @param containerViewId Optional identifier of the container this fragment is
 * to be placed in.  If 0, it will not be placed in a container.
 * @param fragment The fragment to be added.  This fragment must not already
 * be added to the activity.
 * @param tag Optional tag name for the fragment, to later retrieve the
 * fragment with {@link FragmentManager#findFragmentByTag(String)
 * FragmentManager.findFragmentByTag(String)}.
 *
 * @return Returns the same FragmentTransaction instance.
 */
public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment, String tag);
_

同様に、Fragment.onCreateView()は任意のビューを返すことができます。

_/**
 * Called to have the fragment instantiate its user interface view.
 * This is optional, and non-graphical fragments can return null (which
 * is the default implementation).  This will be called between
 * {@link #onCreate(Bundle)} and {@link #onActivityCreated(Bundle)}.
 *
 * <p>If you return a View from here, you will later be called in
 * {@link #onDestroyView} when the view is being released.
 *
 * @param inflater The LayoutInflater object that can be used to inflate
 * any views in the fragment,
 * @param container If non-null, this is the parent view that the fragment's
 * UI should be attached to.  The fragment should not add the view itself,
 * but this can be used to generate the LayoutParams of the view.
 * @param savedInstanceState If non-null, this fragment is being re-constructed
 * from a previous saved state as given here.
 *
 * @return Return the View for the fragment's UI, or null.
 */
@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
_
13