java 在创建片段时使用 ConstraintLayout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43886592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using ConstraintLayout in creating a fragment
提问by mhd.saboori
I'm wondering whether it's possible to create a fragment layout using ConstraintLayout or not!? Is it a proper approach? or is there any standard root element for creating a fragment?
我想知道是否可以使用 ConstraintLayout 创建片段布局!?这是一个正确的方法吗?或者是否有任何用于创建片段的标准根元素?
回答by Doron Yakovlev-Golani
You can use a ConstraintLayout
to contain a Fragment
. It can also be the base view of a Fragment
. However, I usually use a FrameLayout
as a holder because most times there is no reason to use a complex layout such as ConstraintLayout
.
您可以使用 aConstraintLayout
来包含 a Fragment
。它也可以是Fragment
. 但是,我通常使用 aFrameLayout
作为持有人,因为大多数时候没有理由使用复杂的布局,例如ConstraintLayout
.
As you can see, FragmentTransaction.add()
does not put restrictions for the containerViewId
View
.
如您所见,FragmentTransaction.add()
没有对containerViewId
View
.
/**
* 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);
Similarly Fragment.onCreateView()
can return any view:
同样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)