如何在片段中使用上下文控制代碼?


本例演示如何在片段中使用上下文控制代碼

步驟 1 −在 Android Studio 中建立新專案,依次轉到檔案 ⇒ 新建專案,填寫所有必需資訊建立新專案。

步驟 2 −將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <LinearLayout
      android:id="@+id/linearlayout01"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#ccc"
      android:layout_weight="1"
      android:orientation="vertical">
      <fragment android:name="com.example.myapplication.FirstFragment"
         android:id="@+id/frag_1"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" />
   </LinearLayout>
   <LinearLayout
      android:id="@+id/linearlayout02"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:background="#eee"
      android:orientation="vertical">
      <fragment android:name="com.example.myapplication.SecondFragment"
         android:id="@+id/frag_2"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" />
   </LinearLayout>
</LinearLayout>

在上述程式碼中,我們有取了兩個片段。

步驟 3 −將以下程式碼新增到 src/MainActivity.java

package com.example.myapplication;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

步驟 4 −將以下程式碼新增到 src/ FirstFragment.java

package com.example.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FirstFragment extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment, null);
      TextView but = (TextView) root.findViewById(R.id.text);
      but.setText(""+getActivity());
      return root;
   }
}

步驟 4 −將以下程式碼新增到 src/ SecondFragment.java

package com.example.myapplication;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment {
   TextView textView;
   View view;
   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      view = inflater.inflate(R.layout.fragment, container, false);
      return view;
   }
}

執行應用。假設你已將真正的 Android 移動裝置連線到電腦。要從 Android Studio 執行應用,需要開啟專案的一個活動檔案,並點選工具欄中的執行  圖示。選擇你的移動裝置作為選項,然後檢視移動裝置,它將顯示預設螢幕 –

點選 此處 下載專案程式碼

更新時間: 30-7月-2019

2 千+ 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始吧
廣告
© . All rights reserved.