国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

Android 單幀碎片

2018-10-22 15:03 更新

Android 單幀碎片

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


實(shí)例

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

步驟 描述
1 使用 Android Studio IDE 來創(chuàng)建一個(gè) Android 應(yīng)用程序,命名為 Single Fragments,包名 cn.uprogrammer.singlefragments。
2 修改如下所示的主活動(dòng)文件 MainActivity.java。這里我們將要檢查設(shè)備的方向,并基于此切換不同的碎片。
3 在 cn.uprogrammer.singlefragments 包下創(chuàng)建 PortraitFragment.java 和 LandscapeFragment.java 兩個(gè)文件,并關(guān)聯(lián)方法。
4 創(chuàng)建布局文件 res/layout/landscape_fragment.xml 和 res/layout/portrait_fragment.xml 來定義兩個(gè)碎片的布局。
5 修改 res/layout/activity_main.xml 來包含兩個(gè)碎片。
6 在 res/values/strings.xml 中定義需要的常量。
7 啟動(dòng)Android模擬器來運(yùn)行應(yīng)用程序,并驗(yàn)證應(yīng)用程序所做改變的結(jié)果。

下面是主要活動(dòng)文件 src/cn.uprogrammer.singlefragments/MainActivity.java 的內(nèi)容:

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();

        /**
         * 檢測設(shè)備方向,并做相應(yīng)地操作。
         */
        if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            /**
             * 設(shè)備的橫屏模式。
             */
            LandscapeFragment ls_fragment = new LandscapeFragment();
            fragmentTransaction.replace(android.R.id.content, ls_fragment);
        }else{
            /**
             * 設(shè)備的豎屏模式。
             */
            PortraitFragment pm_fragment = new PortraitFragment();
            fragmentTransaction.replace(android.R.id.content, pm_fragment);
        }
        fragmentTransaction.commit();
    }
}

在包 cn.uprogrammer.singlefragments 下創(chuàng)建兩個(gè)碎片文件 LandscapeFragment.java 和 PortraitFragment.java。

以下是 LandscapeFragment.java 文件的內(nèi)容:

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 文件的內(nèi)容:

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 目錄下創(chuàng)建2個(gè)布局文件 landscape_fragment.xml 和 portrait_fragment.xml。

以下是 landscape_fragment.xml 文件的內(nèi)容:

<?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 文件的內(nèi)容:

<?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 文件的內(nèi)容,其中包含兩個(gè)碎片:

<?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 文件包含如下內(nèi)容:

<?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>

讓我們運(yùn)行剛剛修改的 Single Fragments 應(yīng)用程序。我假設(shè)你已經(jīng)在安裝環(huán)境時(shí)創(chuàng)建了 AVD。打開你的項(xiàng)目中的活動(dòng)文件,點(diǎn)擊工具欄中的 圖標(biāo)來在 Android Studio 中運(yùn)行應(yīng)用程序。Android Studio 在 AVD 上安裝應(yīng)用程序并啟動(dòng)它。如果一切順利,將在模擬器窗口上顯示如下:

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

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

當(dāng)你修改模式,你將看到適用于橫屏模式的頁面實(shí)現(xiàn):

通過這種方法,你可以在同一個(gè)活動(dòng)中通過使用不用的碎片來實(shí)現(xiàn)不同的界面??梢园凑漳愕男枨笫褂貌煌愋偷慕缑娼M件來構(gòu)建界面。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)