bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

如何在Android中動態(tài)添加碎片

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何在Android中動態(tài)添加碎片,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的梅州網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

1.新建一個碎片布局,fragment.xml

<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是碎片1"/>
</LinearLayout>

2. 新建一個類Fragment1.java,繼承自Fragment

注意Fragment有兩個不同的包,推薦使用support-v4中的,兼容性更好,另一個安卓4.2以下就會崩潰。在該碎片中可以進(jìn)行各種操作,就如同操作一個activity。

public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_questions1,container,false);
Log.d("questionMain1","碎片1加載");
return view;
}
}

碎片和活動之間的通信。雖然碎片都是嵌入在活動中顯示的,但他們之間的關(guān)系并不明顯。

1.在活動中調(diào)用碎片的方法。FragmentManagert提供了一個類似于finViewById()的方法,用于從布局文件中獲取碎片的實例。如果是動態(tài)加載的就跟簡單了加載是你就有了該碎片的實例。

2.在碎片中調(diào)用活動的方法??梢酝ㄟ^getActivity()方法得到和當(dāng)前碎片綁定的活動實例。

碎片的綁定

1.靜態(tài)綁定

在活動布局中加一個碎片標(biāo)簽,比較簡單不細(xì)說。android:name="",該標(biāo)簽為碎片對應(yīng)的類,注意要包含路徑全名。

<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="這是碎片3"/>
<fragment
android:id="@+id/fragment1"
android:name="com.example.fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

2.動態(tài)綁定

這個才是碎片的強(qiáng)大之處,在程序運(yùn)行時動態(tài)的添加到碎片中,根據(jù)具體情況來動態(tài)添加碎片,可以將程序界面定制得更加多樣化(多用于自適應(yīng)手機(jī)和平板的應(yīng)用)

下面的代碼以點(diǎn)擊按鈕。有三個碎片,通過點(diǎn)擊事件在一個活動中動態(tài)切換顯示的碎片。

package com.xiaobu.xiaoyan1.question;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;
import com.xiaobu.xiaoyan1.R;
import com.xiaobu.xiaoyan1.base.BaseActivity;
public class QuestionsMain extends BaseActivity implements TextView.OnClickListener{
private TextView fragment1;
private TextView fragment2;
private TextView fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_main);
initView();
}
private void initView(){
((TextView)findViewById(R.id.question_text)).setTextColor(getResources().getColor(R.color.colorTextChecked));
fragment1=(TextView)findViewById(R.id.quiz_text_view);
fragment2=(TextView)findViewById(R.id.answer_text_view);
fragment3=(TextView)findViewById(R.id.chosen_text_view);
fragment1.setOnClickListener(this);
fragment2.setOnClickListener(this);
fragment3.setOnClickListener(this);
changeFragment(new QuestionsMain1());
checkedChange(fragment1);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.quiz_text_view:
changeFragment(new QuestionsMain1());
break;
case R.id.answer_text_view:
changeFragment(new QuestionsMain2());
break;
case R.id.chosen_text_view:
changeFragment(new QuestionsMain3());
break;
default:
break;
}
}
private void changeFragment(Fragment fragment){
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.main_view,fragment);//第一個參數(shù)表示容器的id,第二個參數(shù)為碎片實例。
transaction.commit();
}
}

上述就是小編為大家分享的如何在Android中動態(tài)添加碎片了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前題目:如何在Android中動態(tài)添加碎片
URL鏈接:http://vcdvsql.cn/article30/jhieso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管商城網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計公司虛擬主機(jī)網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)