Android開發學習—— Fragment,androidfragment

來源:互聯網
上載者:User

Android開發學習—— Fragment,androidfragment

#Fragment
* 用途:在一個Activity裡切換介面,切換介面時只切換Fragment裡面的內容
* 生命週期方法跟Activity一致,可以理解把其為就是一個Activity
* 定義布局檔案作為Fragment的顯示內容

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void click01(View v){        fragment fg1 = new fragment();        FragmentManager fm = getFragmentManager();        FragmentTransaction ft = fm.beginTransaction();        ft.replace(R.id.f1,fg1);        ft.commit();    }    public void click02(View v){        fragment1 fg2 = new fragment1();        FragmentManager fm = getFragmentManager();        FragmentTransaction ft = fm.beginTransaction();        ft.replace(R.id.f1,fg2);        ft.commit();    }    public void click03(View v){        fragment2 fg3 = new fragment2();        FragmentManager fm = getFragmentManager();        FragmentTransaction ft = fm.beginTransaction();        ft.replace(R.id.f1,fg3);        ft.commit();    }}
public class fragment1 extends Fragment {    @Nullable    @Override    //返回的view對象會作為fragment的內容顯示在螢幕上    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {       View v =  inflater.inflate(R.layout.fragment1,null);        return  v;    }}

 

<?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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context="com.example.xnmeng.hello.MainActivity">   <LinearLayout       android:layout_width="wrap_content"       android:layout_height="match_parent"       android:orientation="vertical">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragment01"        android:onClick="click01"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragment02"        android:onClick="click02"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="fragment03"        android:onClick="click03"/>   </LinearLayout>    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/f1">    </FrameLayout></LinearLayout>

 

<?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"    android:background="#ff0000">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="紅色"/></LinearLayout>

結果:

點擊左邊的三個按鈕,右邊顯示相應的顏色。


###生命週期
* fragment切換時舊fragment對象會銷毀,新的fragment對象會被建立
###低版本相容
* 在support-v4.jar包中有相關api,也就是說fragment可以在低版本模擬器運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.