Latest web development tutorials

Android 單幀碎片

單幀碎片 :單幀碎片是為小屏幕設備所設計的,如手持設備(移動電話),Android 3.0以上版本支持。


實例

該實例解釋如何創建自己的碎片。 這裡我們創建兩個碎片,其中一個被使用在設備是橫屏的時候,另一個被使用在設備是豎屏的時候。 下面讓我們按照步驟開始吧。

步驟 描述
1 使用Android Studio IDE 來創建一個Android 應用程序,命名為Single Fragments,包名cn.uprogrammer.singlefragments。
2 修改如下所示的主活動文件MainActivity.java。 這裡我們將要檢查設備的方向,並基於此切換不同的碎片。
3 在cn.uprogrammer.singlefragments 包下創建PortraitFragment.java 和LandscapeFragment.java 兩個文件,並關聯方法。
4 創建佈局文件res/layout/landscape_fragment.xml 和res/layout/portrait_fragment.xml 來定義兩個碎片的佈局。
5 修改res/layout/activity_main.xml 來包含兩個碎片。
6 在res/values/strings.xml 中定義需要的常量。
7 啟動Android模擬器來運行應用程序,並驗證應用程序所做改變的結果。

下面是主要活動文件src/cn.uprogrammer.singlefragments/MainActivity.java 的內容:

package cn.uprogrammer.singlefragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Configuration config = getResources().getConfiguration();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction =
                fragmentManager.beginTransaction();

        /**
         * 检测设备方向,并做相应地操作。
         */
        if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            /**
             * 设备的横屏模式。
             */
            LandscapeFragment ls_fragment = new LandscapeFragment();
            fragmentTransaction.replace(android.R.id.content, ls_fragment);
        }else{
            /**
             * 设备的竖屏模式。
             */
            PortraitFragment pm_fragment = new PortraitFragment();
            fragmentTransaction.replace(android.R.id.content, pm_fragment);
        }
        fragmentTransaction.commit();
    }
}

在包cn.uprogrammer.singlefragments 下創建兩個碎片文件LandscapeFragment.java 和PortraitFragment.java。

以下是LandscapeFragment.java 文件的內容:

package cn.uprogrammer.singlefragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class LandscapeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        /**
         * Inflate the layout for this fragment
         */
        return inflater.inflate(
                R.layout.landscape_fragment, container, false);
    }

}

以下是PortraitFragment.java 文件的內容:

package cn.uprogrammer.singlefragment;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PortraitFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        /**
         * Inflate the layout for this fragment
         */
        return inflater.inflate(
                R.layout.portrait_fragment, container, false);
    }
}

在目錄res/layout 目錄下創建2個佈局文件landscape_fragment.xml 和portrait_fragment.xml。

以下是landscape_fragment.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="28sp" />

    <!-- More GUI components go here  -->

</LinearLayout>

以下是portrait_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="28sp" />

    <!-- 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:id="@+id/landscape_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/portrait_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">Single Fragment</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="landscape_message">这是横屏模式碎片</string>
    <string name="portrait_message">这是竖屏模式碎片</string>
</resources>

讓我們運行剛剛修改的Single Fragments 應用程序。 我假設你已經在安裝環境時創建了AVD。 打開你的項目中的活動文件,點擊工具欄中的 圖片 圖標來在Android Studio 中運行應用程序。 Android Studio 在AVD 上安裝應用程序並啟動它。 如果一切順利,將在模擬器窗口上顯示如下:

圖片

按照下列操作來改變模擬器屏幕的方向模式:

  • fn+control+F11 在mac上改變橫屏為豎屏,反之亦然
  • ctrl+F11 在windows上
  • ctrl+F11 在Linux上

當你修改模式,你將看到適用於橫屏模式的頁面實現:

圖片

通過這種方法,你可以在同一個活動中通過使用不用的碎片來實現不同的界面。 可以按照你的需求使用不同類型的界面組件來構建界面。