您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Adroid中Fragment的簡(jiǎn)單使用

發(fā)布時(shí)間:2020-08-10 17:36:18 來(lái)源:網(wǎng)絡(luò) 閱讀:488 作者:hello_world007 欄目:移動(dòng)開(kāi)發(fā)

下面四個(gè)步驟就能創(chuàng)建一個(gè)簡(jiǎn)單的fragment

1. 擴(kuò)展Fragment class

2. 在XML 或 Java中提供顯示

3. 覆蓋onCreateView方法

4. 在activity中使用Fragment


如下就是簡(jiǎn)單的顯例

創(chuàng)建一個(gè)FirstActivityFragment.java文件,擴(kuò)展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)建一個(gè)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>

并在一個(gè)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>


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI