Single frame fragment是为小屏幕设备(如手持设备)设计的,并且应高于android 3.0版本。
示例
本示例将向您说明如何创建自己的fragment。在这里,我们将创建两个fragment,其中一个fragment将在设备处于横向模式时使用,另一个fragment将在纵向模式下使用。
以下是修改后的主要Activity文件 MainActivity.java 的内容-
package com.example.myfragments; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.res.Configuration; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Configuration config = getResources().getConfiguration(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); /** * Check the device orientation and act accordingly */ if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { /** * Landscape mode of the device */ LM_Fragement ls_fragment = new LM_Fragement(); fragmentTransaction.replace(android.R.id.content, ls_fragment); }else{ /** * Portrait mode of the device */ PM_Fragement pm_fragment = new PM_Fragement(); fragmentTransaction.replace(android.R.id.content, pm_fragment); } fragmentTransaction.commit(); } }
创建两个fragment文件 LM_Fragement.java 和 PM_Fragment.java
以下是 LM_Fragement.java 文件的内容-
package com.example.myfragments; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by LearnFk7 on 8/23/2016. */ public class LM_Fragement extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate(R.layout.lm_fragment, container, false); } }
以下是 PM_Fragement.java 文件的内容-
package com.example.myfragments; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by LearnFk7 on 8/23/2016. */ public class PM_Fragement extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** * Inflate the layout for this fragment */ return inflater.inflate(R.layout.pm_fragment, container, false); } }
在 res/layout 目录下创建两个布局文件 lm_fragement.xml 和 pm_fragment.xml ,以下是 lm_fragement.xml 文件的内容-
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#7bae16"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/landscape_message" android:textColor="#000000" android:textSize="20px" /> <!-- More GUI components go here --> </LinearLayout>
以下是 pm_fragment.xml 文件的内容-
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#666666"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/portrait_message" android:textColor="#000000" android:textSize="20px" /> <!-- More GUI components go here --> </LinearLayout>
以下是 res/layout/activity_main.xml 文件的内容,其中包括您的fragment-
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <fragment android:name="com.example.fragments" android:id="@+id/lm_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.fragments" android:id="@+id/pm_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
确保您具有 res/values/strings.xml 文件的以下内容-
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My Application</string> <string name="landscape_message">This is Landscape mode fragment</string> <string name="portrait_message">This is Portrait mode fragment></string> </resources>
让我们尝试运行刚刚创建的修改后的 MyFragments 应用程序,我假设您在进行环境设置时创建了 AVD 。要从Android Studio运行该应用,请打开您项目的Activity文件之一,然后从工具栏中单击"运行"图标。 Android Studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示"Emulator"窗口,您将在其中单击"菜单"按钮以查看以下窗口。请耐心等待,因为这可能要花一些时间,具体取决于您的计算机速度-
要更改仿真器屏幕的模式,请执行以下操作-
- 在Mac上
fn + control + F11 可以将横向更改为纵向,反之亦然。
在Windows上是ctrl + F11 。
在Linux上是ctrl + F11 。
更改模式后,您将能够看到为横向模式实现的GUI,如下所示-
这样,您可以通过不同的fragment使用相同的Activity,但使用不同的GUI。您可以根据需要将不同类型的GUI组件用于不同的GUI。
参考链接
https://www.learnfk.com/android/android-single-fragments.html