日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android fragment 菜单栏,android UI:底部菜单栏的学习与制作——Fragment碎片一

發(fā)布時間:2023/12/19 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android fragment 菜单栏,android UI:底部菜单栏的学习与制作——Fragment碎片一 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

碎片(Fragment) 嵌入與活動中的UI片段,為了合理的分配布局而存在,這是我的簡單理解。多用于兼顧手機(jī)與平板的UI,也適用于靈活高級的UI制作。

Demo 簡單的按鍵切換兩片不同的Demo

新建left_fragment.xml

android:id="@+id/button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="Button"

/>

新建right_fragment.xml

android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/>

新建another_right_fragment.xml

android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:textSize="20sp"android:text="This is right fragment"/>

main_Activity.xml

>

android:layout_width="0dp"android:name="test.example.com.fragmenttest.LeftFragment"android:layout_height="match_parent"android:layout_weight="1"android:id="@+id/left_fragment"/>

android:id="@+id/right_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1">

分別新建對應(yīng)的類

packagetest.example.com.fragmenttest;importandroid.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/

public class LeftFragment extendsFragment {

@OverridepublicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

View view=inflater.inflate(R.layout.lift_fregment,container,false);returnview;

}

}

packagetest.example.com.fragmenttest;//import android.app.Fragment;

importandroid.support.v4.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/

public class RightFragment extendsFragment{

@OverridepublicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

View view=inflater.inflate(R.layout.right_fragment,container,false);returnview;

}

}

packagetest.example.com.fragmenttest;importandroid.support.v4.app.Fragment;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;/*** Created by hs769 on 2017/4/4.*/

public class AnotherRightFragment extendsFragment {

@OverridepublicView onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

View view=inflater.inflate(R.layout.another_right_fragment,container,false);returnview;

}

}

packagetest.example.com.fragmenttest;importandroid.support.v4.app.Fragment;importandroid.support.v4.app.FragmentManager;importandroid.support.v4.app.FragmentTransaction;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;public class MainActivity extends AppCompatActivity implementsView.OnClickListener{

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button=(Button)findViewById(R.id.button);

button.setOnClickListener(this);

replaceFragment(newRightFragment());

}

@Overridepublic voidonClick(View v) {switch(v.getId()){caseR.id.button:

replaceFragment(newAnotherRightFragment());break;default:break;

}

}private voidreplaceFragment(Fragment fragment){

FragmentManager fragmentManager=getSupportFragmentManager();

FragmentTransaction transaction=fragmentManager.beginTransaction();

transaction.replace(R.id.right_layout,fragment);

transaction.commit();

}

}

LeftFragment,RightFragment和another_Right_Fragment這三個類分別extends(繼承)Fragment類,這是一個關(guān)鍵,因為有兩個包中含有Fragment,建議選擇android.support.v4.app.Fragment

如果包選擇不一樣會出現(xiàn)如下錯誤(MainAcitvity.java),如圖更改即可:找到出問題的類,更換包,完畢

最終效果實現(xiàn)點擊button切換碎片(下圖為點擊前后的變化,分別為兩個Fragment)

總結(jié)

以上是生活随笔為你收集整理的android fragment 菜单栏,android UI:底部菜单栏的学习与制作——Fragment碎片一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。