Android UI Android碎片

2018-04-01 10:33 更新

在小屏幕設(shè)備中,activity通常填充整個(gè)屏幕。而這個(gè)activity是一個(gè)視圖的容器。

為了更好地在平板電腦上組織UI,我們可以使用“mini-activities”。每個(gè)mini-activities包含自己的一組視圖。

一個(gè)activity可以包含一個(gè)或多個(gè)這些mini-activities。這些mini-activities被稱為碎片(Fragment)。

碎片可以包含視圖,就像activity一樣。碎片總是會(huì)嵌入在一個(gè)activity中。

碎片形成用戶界面的原子單位,可以在activity中動(dòng)態(tài)添加或刪除。

我們可以將Android中的片段的概念視為桌面用戶界面中的面板。

添加碎片

以下代碼顯示了碎片的基本用法。

res/layout 文件夾中,添加一個(gè)新文件并將其命名為 fragment1.xml 。填寫(xiě)以下內(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="#00FF00"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #1"
        android:textColor="#000000"
        android:textSize="25sp" />
</LinearLayout>

另外在 res/layout 文件夾中,添加另一個(gè)新文件,并將其命名為 fragment2.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="#FFFE00"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is fragment #2"
        android:textColor="#000000"
        android:textSize="25sp" />
</LinearLayout>

activity_main.xml 中,添加以下代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <fragment
        android:name="cn.w3cschool.myapplication3.app.Fragment1"
        android:id="@+id/fragment1"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
    <fragment
        android:name="cn.w3cschool.myapplication3.app.Fragment2"
        android:id="@+id/fragment2"
        android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />

</LinearLayout>

com.java2s.Fragments 包名稱下,添加兩個(gè)Java類文件并將其命名為 Fragment1.java Fragment2.java,將以下代碼添加到 Fragment1.java

package cn.w3cschool.myapplication3.app;

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

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

將以下代碼添加到 Fragment2.java :

package cn.w3cschool.myapplication3.app;
//  hgci.cn
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        //Inflate the layout for this fragment
        return inflater.inflate(
                R.layout.fragment2, container, false);
    }
}
Android碎片

注意

一個(gè)碎片的行為非常像一個(gè)activity,它有:

  • 一個(gè)Java類
  • 它從XML文件加載其UI。

XML文件包含你預(yù)期從某個(gè)activity獲得的所有常用UI元素:TextView,EditText,Button等。

碎片的Java類需要擴(kuò)展 Fragment 基類:

public class Fragment1 extends Fragment {
}

除了 Fragment 基類,碎片還可以擴(kuò)展一些Fragment 類的其他幾個(gè)子類,例如 DialogFragment,ListFragmentPreferenceFragment 。

要繪制碎片的UI,請(qǐng)覆蓋 onCreateView()方法。此方法需要返回一個(gè)View對(duì)象。

你可以使用LayoutInflater對(duì)象從指定的XML文件中擴(kuò)充UI。要向activity添加片段,請(qǐng)使<fragment>元素。

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="horizontal" >
/*  hgci.cn*/
          <fragment
              android:name="cn.w3cschool.Fragments.Fragment1"
              android:id="@+id/fragment1"
              android:layout_weight="1"
              android:layout_width="0px"
              android:layout_height="match_parent" />
          <fragment
              android:name="cn.w3cschool.Fragments.Fragment2"
              android:id="@+id/fragment2"
              android:layout_weight="1"
              android:layout_width="0px"
              android:layout_height="match_parent" />

      </LinearLayout>

每個(gè)碎片需要唯一的標(biāo)識(shí)符。



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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)