Android - 单帧片段
单帧片段
单帧片段专为手持设备(手机)等小屏幕设备设计,应在android 3.0以上版本。
示例
这个例子将向您解释如何创建自己的Fragments。在这里,我们将创建两个片段,其中一个将在设备处于横向模式时使用,另一个片段将用于纵向模式。 因此,让我们按照以下步骤类似于我们在创建 Hello World 示例 时所遵循的步骤 −
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android StudioIDE 创建一个 Android 应用程序,并将其命名为 MyFragments,位于包 com.example.myfragments 下,Activity 为空白。 |
| 2 | 修改主活动文件MainActivity.java,如下代码所示。 在这里,我们将检查设备的方向,因此我们将在不同的片段之间切换。 |
| 3 | 在包 com.example.myfragments 下创建两个 java 文件 PM_Fragment.java 和 LM_Fragement.java 来定义您的片段和相关方法。 |
| 4 | 创建布局文件 res/layout/lm_fragment.xml 和 res/layout/pm_fragment.xml 并为这两个片段定义布局。 |
| 5 | 修改 res/layout/activity_main.xml 文件的默认内容以包含两个片段。 |
| 6 | 在 res/values/strings.xml 文件中定义所需的常量 |
| 7 | 运行应用程序以启动 Android 模拟器并验证应用程序中所做更改的结果。 |
以下是修改后的主活动文件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();
}
}
创建两个片段文件 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 TutorialsPoint7 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 TutorialsPoint7 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 文件的内容,其中包括您的片段 −
<?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 运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的运行图标。 Android Studio 会在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示模拟器窗口,您将在其中单击菜单按钮以查看以下窗口。请耐心等待,因为根据您的计算机速度可能需要一些时间 −
要更改模拟器屏幕的模式,让我们执行以下操作 −
fn+control+F11 在 Mac 上将横向更改为纵向,反之亦然。
ctrl+F11 在 Windows 上。
ctrl+F11 在 Linux 上。
更改模式后,您将能够看到为横向模式实现的 GUI,如下所示 −
这样,您可以通过不同的片段使用相同的活动但不同的 GUI。 您可以根据您的要求为不同的 GUI 使用不同类型的 GUI 组件。