標籤:推薦 nbsp this rri set div 是你 執行個體 tco
要使用片段先要建立一個片段,建立一個片段很簡單。
- 建立一個片段布局,fragment.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是片段1"/></LinearLayout>
2. 建立一個類Fragment1.java,繼承自Fragment,注意Fragment有兩個不同的包,推薦使用support-v4中的,相容性更好,另一個安卓4.2以下就會崩潰。在該片段中可以進行各種操作,就如同操作一個activity。
public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_questions1,container,false); Log.d("questionMain1","片段1載入"); return view; }}
片段和活動之間的通訊。雖然片段都是嵌入在活動中顯示的,但他們之間的關係並不明顯。
1.在活動中調用片段的方法。FragmentManagert提供了一個類似於finViewById()的方法,用於從布局檔案中擷取片段的執行個體。如果是動態載入的就跟簡單了載入是你就有了該片段的執行個體。
2.在片段中調用活動的方法。可以通過getActivity()方法得到和當前片段綁定的活動執行個體。
- 靜態繫結
在活動布局中加一個片段標籤,比較簡單不細說。android:name="",該標籤為片段對應的類,注意要包含路徑全名。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是片段3"/> <fragment android:id="@+id/fragment1" android:name="com.example.fragment1" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
2.動態綁定,這個才是片段的強大之處,在程式運行時動態添加到片段中,根據具體情況來動態添加片段,可以將程式介面定製得更加多樣化(多用於自適應手機和平板的應用)
下面的代碼以點擊按鈕。有三個片段,通過點擊事件在一個活動中動態切換顯示的片段。
package com.xiaobu.xiaoyan1.question;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.widget.TextView;import com.xiaobu.xiaoyan1.R;import com.xiaobu.xiaoyan1.base.BaseActivity;public class QuestionsMain extends BaseActivity implements TextView.OnClickListener{ private TextView fragment1; private TextView fragment2; private TextView fragment3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_question_main); initView(); } private void initView(){ ((TextView)findViewById(R.id.question_text)).setTextColor(getResources().getColor(R.color.colorTextChecked)); fragment1=(TextView)findViewById(R.id.quiz_text_view); fragment2=(TextView)findViewById(R.id.answer_text_view); fragment3=(TextView)findViewById(R.id.chosen_text_view); fragment1.setOnClickListener(this); fragment2.setOnClickListener(this); fragment3.setOnClickListener(this); changeFragment(new QuestionsMain1()); checkedChange(fragment1); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.quiz_text_view: changeFragment(new QuestionsMain1()); break; case R.id.answer_text_view: changeFragment(new QuestionsMain2()); break; case R.id.chosen_text_view: changeFragment(new QuestionsMain3()); break; default: break; } } private void changeFragment(Fragment fragment){ FragmentManager fragmentManager=getSupportFragmentManager(); FragmentTransaction transaction=fragmentManager.beginTransaction(); transaction.replace(R.id.main_view,fragment);//第一個參數表示容器的id,第二個參數為片段執行個體。 transaction.commit(); }}
安卓動態添加片段