下面四個步驟就能創(chuàng)建一個簡單的fragment
10年積累的網站制作、成都網站建設經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站設計制作后付款的網站建設流程,更有淮陰免費網站建設讓你可以放心的選擇與我們合作。
1. 擴展Fragment class
2. 在XML 或 Java中提供顯示
3. 覆蓋onCreateView方法
4. 在activity中使用Fragment
如下就是簡單的顯例
創(chuàng)建一個FirstActivityFragment.java文件,擴展Fragment class
package com.example.liang.login; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.VelocityTrackerCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; /** * Created by liang on 2016/7/15. */ public class FirstActivityFragment extends Fragment { public FirstActivityFragment(){ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View v = inflater.inflate(R.layout.fragment_first, container, false); TextView textView = (TextView)v.findViewById(R.id.showFirstFragmentInfo); final Context context = this.getActivity(); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast hello; hello = Toast.makeText(context,"hello",Toast.LENGTH_LONG); hello.show(); } }); return v; } @Override public void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
為Fragment創(chuàng)建一個xml fragment_first.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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/showFirstFragmentInfo" android:text="this is a firstFragment"/> </LinearLayout>
并在一個activity view 中使用
<?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" android:orientation="vertical" android:background="#33FF00"> <fragment xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment" android:name="com.example.liang.login.FirstActivityFragment" tools:layout="@layout/fragment_first" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
本文標題:Adroid中Fragment的簡單使用
標題鏈接:http://chinadenli.net/article34/pihhse.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站建設、網站改版、App開發(fā)、電子商務、小程序開發(fā)、企業(yè)建站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)