1:什么是Fragment,Fragment是一種可以嵌入在活動中的UI片段,能夠讓程式更加合理和充分地利用大螢屏的空間,出現的初衷是為了適應大螢屏的平板電腦,可以將其看成一個小型Activity,又稱作Activity片段,
2:Fragment依賴于Activity,不能獨立存在,一個Activity可以有多個Fragment,一個Fragment可以被多個Activity重用,Fragment有自己的生命周期,并能接收輸入事件,可以在Activity運行時動態地添加或洗掉Fragment

3:Fragment的生命周期
生命周期我們下次再來細說,

3:接下來就寫一個比較簡單的實體
(1)activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center">
<TextView
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Hello World!" />
</LinearLayout>
<fragment
android:id="@+id/fargment_id"
android:name="com.example.myapplication.FragmentButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"/>
(2)fragment_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:gravity="center">
<Button
android:id="@+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="click" />
</LinearLayout>
</LinearLayout>
(3)FragmentButton類
public class FragmentButton extends Fragment implements View.OnClickListener {
private Button button;
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_button,null);
button = (Button) view.findViewById(R.id.button_id);
button.setOnClickListener(this);
//這里就是把view回傳給MainActivity里的方法
return view;
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_id:
//在Fragment中使用Activity中控制元件的方式
// 在當前的Fragment中呼叫getActivity方法獲取依附著的那個Activity,
// 然后再用獲取到的Activity去findViewById拿到你需要的控制元件對其操作就行了,
AppCompatActivity activity = (AppCompatActivity) getActivity();
textView = (TextView) activity.findViewById(R.id.text_id);
textView.setText("我是 fragment Button");
break;
default:
break;
}
}
}
(4)MainActivity類
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
5:運行結果

點擊按鈕

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/277705.html
標籤:其他
