android開發之Fragment載入到一個Activity中,androidfragment

來源:互聯網
上載者:User

android開發之Fragment載入到一個Activity中,androidfragment

Fragments 是android3.0以後添加的。主要是為了方便android平板端的開發。方便適應不同大小的螢幕。此代碼是為了最簡單的Fragment的使用,往一個Activity中添加Fragment,主要涉及的知識點有:1、Fragment類的建立,2、Fragment的添加3、無UI的 Fragment的添加,根據Tag找回Fragment

     Fragment對應的Xml布局檔案, <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity"    android:weightSum="10" >     <Button         android:id="@+id/bt"        android:layout_width="match_parent"        android:layout_height="0dp"         android:text="添加一個Fragment"        android:layout_weight="2"/>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"               android:text="將Fragment載入到Activity中,此Fragment中沒有UI,即不需要實現onCreateView方法,可以當做此Activity的背景色" />     <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"              android:text="根據Fragment的Tag找到Fragment" />    <LinearLayout         android:id="@+id/lv_fragment_container"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="9"        android:background="#123456"        android:orientation="horizontal">     </LinearLayout></LinearLayout> 1、Fragment的建立 package com.example.fragment1; import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup; /** * @author sea * 建立一個Fragment至少要實現三個生命週期函數onCreate,onCreateView,onPause * */public class MyFragment extends Fragment {     /*      * 初始化Fragment,執行個體化在Fragment中的成員變數     */    @Override    public void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);    }     /*      * 給Fragment 載入UI的布局,返回Fragment布局檔案對應的東東     */    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        // TODO Auto-generated method stub        View view = inflater.inflate(R.layout.fragment,container, false);         return view;    }     /*      * 當使用者離開此Fragment時調用     */    @Override    public void onPause() {        // TODO Auto-generated method stub        super.onPause();    } } 2、無UI的Fragment的建立 package com.example.fragment1; import android.app.Fragment;import android.os.Bundle;  public class MyFragment2 extends Fragment {     @Override    public void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);     }    @Override    public void onPause() {        // TODO Auto-generated method stub        super.onPause();    }} 3、Fragment的添加到Activity中 package com.example.fragment1; import android.os.Bundle;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button; /** * @author sea * 將fragment載入到一個Activity中 * 方法一代碼:如此例子主要是用到FragmentTransaction類 * 方法二:直接在xml檔案中添加 * */public class MainActivity extends Activity {     private Button button;     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.bt);        button.setOnClickListener(new OnClickListener() {             @Override            public void onClick(View arg0) {                 //找到FragmentTransaction                FragmentManager fragmentManager = getFragmentManager();                FragmentTransaction fragmentTransaction = fragmentManager.                        beginTransaction();                MyFragment fragment = new MyFragment();                //加到Activity中                fragmentTransaction.add(R.id.lv_fragment_container,fragment);                //加到後台堆棧中,有下一句代碼的話,點擊返回按鈕是退到Activity介面,沒有的話,直接退出Activity                //後面的參數是此Fragment的Tag。相當於id                fragmentTransaction.addToBackStack("fragment1");                //記住提交                fragmentTransaction.commit();             }        });    }}
android開發 怎從fragment中通過控制項跳到另一個activity

intent in=new intent(getactivity(),要跳轉的activity);
 
Android 新特性的fragment,在其中怎通過按鈕跳轉到其他與之無關的activity

在fragment中可以getActivity(),用這個就可以了
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.