Android Studio 中的 fragment_main 和 activity_main 布局 [英] fragment_main and activity_main layouts in Android Studio

查看:44
本文介绍了Android Studio 中的 fragment_main 和 activity_main 布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android Studio 生成的两个布局文件感到困惑.(fragment_main.xml 和activity_main.xml)

我使用activity_main.xml.要使用activity_main.xml,我需要注释掉

if(savedInstanceState==null){...}//在onCreate()中,否则不显示

然后我可以使用activity_main.xml.

谁能帮我解释一下

  1. 何时使用 Fragment_main.xml

  2. 使用两种布局的优势.

  3. 如何正确使用它们.(如果我不注释掉上面的一行,它不会显示activity_main.xml,而是显示fragment_main 布局.这意味着我需要在fragment_main.xml 中创建接口.)

谢谢.

解决方案

以下是使用 Fragments(Fragmenting your Activities) 的好处:

  1. 多模式支持:就像您的应用程序支持横向和纵向模式或平板设备一样.考虑像您有一个列表视图,另一个是您的应用程序中的详细信息页面.您可以制作两个片段,一个带有列表视图,一个带有详细信息而不是两个活动,并且可以在横向和平板电脑设备中组合在一起,而不是为平板电脑制作另一个活动.在平板电脑上查看 android Settings 屏幕.

  2. 自定义视图:在某些情况下,我们需要创建一些自定义视图,但自定义视图的问题是维护状态,因此如果它们符合您的需要,您可以使用片段.

  3. 应用导航:滑动抽屉导航处理.在您的设备中打开 Google Play 检查滑动导航.如果您点击滑动导航栏中的任何可用选项,您将不会看到点击项目时启动的任何活动.由于它们都是应用程序的顶视图,在这种情况下,您可以拥有一个带有片段的 Activity 并在点击时更改它们.

  4. 重用视图:创建片段后,您可以在运行时在任何活动中使用它们,并将它们附加到您的活动中.

还有许多其他好处,一旦开始使用,您就会发现它们.

android developer's space 上有一个很好的解释:

http://developer.android.com/guide/components/fragments.html>

阅读本文并做出决定.

I am confused with the two layout files that are generated by the Android Studio. (fragment_main.xml and activity_main.xml)

I use activity_main.xml. To use activity_main.xml I need to comment out

if(savedInstanceState==null){...} //in onCreate(), Otherwise it will not display

Then I can use activity_main.xml.

Can some one explain me

  1. when to use Fragment_main.xml

  2. Advantage of using both layouts.

  3. How to use them correctly.(If I do not comment out above line it will not display activity_main.xml, instead it shows fragment_main layout. That means I need to create the interface in the fragment_main.xml.)

Thanks.

解决方案

Following are the benefits using Fragments(Fragmenting your Activities) :

  1. Multiple mode supports : Like if your application supports landscape and portrait mode or tablet devices. Consider something like you have a list view and another is the details page in your app. You can make two fragments one with list view and one with details instead of two activities and can group together in landscape and in tablet devices instead of making an another activity for tablet. Have a look on android Settings screen in tablet.

  2. Custom Views : There are some scenarios where we need to create some custom views but the issue with custom views are maintaining the state so instead you can use fragment if they fits in your needs.

  3. App Navigation : Sliding drawer navigation handling. Open Google Play in your device an check the sliding navigation. If you tap on any of the option available in sliding navigation bar, you wont see any activity started on tapping on the items. As they all are top view of app,in such scenarios you can have an Activity with fragment and change them on tap .

  4. Reuse the views : Once you Create your Fragment you can use them in any activity at run time attaching them in your activity.

There are many other benefits, you will find them once you start using.

There is a nice explanation over here on android developer's space :

http://developer.android.com/guide/components/fragments.html

Read this and decide.

这篇关于Android Studio 中的 fragment_main 和 activity_main 布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆