java 为什么 FrameLayout 用于片段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17495199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:10:34  来源:igfitidea点击:

Why is a FrameLayout used for fragments?

javaandroidfragmentandroid-framelayout

提问by maysi

Wherever I look, FrameLayoutseems to be used as the FragmentContainer. Why is FrameLayoutalways seen with Fragments?

无论我在哪里,FrameLayout似乎都被用作FragmentContainer. 为什么FrameLayout总是看到与Fragments

回答by h4rd4r7c0r3

You can basically use RelativeLayoutor LinearLayoutit will still work,but the answer of your question is in FrameLayout's documentation :

您基本上可以使用RelativeLayout或者LinearLayout它仍然可以工作,但是您的问题的答案在FrameLayout的文档中:

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

FrameLayout 旨在在屏幕上遮挡一个区域以显示单个项目。通常,FrameLayout 应该用于保存单个子视图,因为很难以可缩放到不同屏幕尺寸的方式组织子视图,而子视图又不会相互重叠。但是,您可以使用 android:layout_gravity 属性将多个子项添加到 FrameLayout 并通过为每个子项分配重力来控制它们在 FrameLayout 中的位置。

You can read more about FrameLayouthere : FrameLayout/Android Developers

您可以FrameLayout在此处阅读更多信息:FrameLayout/Android Developers

回答by Sumit Dhaniya

Main purpose of frame layout is to block the area required to fit the largest child view. If you use a Frame Layout as Fragment Container you can ensure that you always have the space available to accommodate the largest fragment layout.

框架布局的主要目的是阻止适合最大子视图所需的区域。如果您使用 Frame Layout 作为 Fragment Container,您可以确保始终有可用空间来容纳最大的 Fragment 布局。

In some cases you may need to have more than 1 fragment on screen simultaneously in that case you should prefer Relative or Linear Layout.

在某些情况下,您可能需要同时在屏幕上显示 1 个以上的片段,在这种情况下,您应该更喜欢相对或线性布局。

回答by kaushal trivedi

Everything is based on user requirements and customization needed.

一切都基于用户要求和所需的定制。

It is not necessary to use FrameLayout at all.

完全没有必要使用 FrameLayout。

For example in below code there is no layout taken , and fragment itself only contains single imageview .

例如在下面的代码中没有采用布局,片段本身只包含单个 imageview 。

public class MyFragment extends Fragment implements OnClickListener{

    String TAG="MyFragment";
    Context c;
    MyFragment(Context con){
    c=con;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        ImageView v=new ImageView(c);
            //other imageview stuff

        return v;
    }
    @Overrides
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onClcik");

    }
}