生活随笔
收集整理的這篇文章主要介紹了
Android学习笔记之Fragment的两种使用方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、第一種方法:
(1)Fragment的第一種使用方法是使用fragment加載單獨的布局文件:(也就是xml的方式實現(xiàn))
結構如下:
activity_main.xml主要是在一個線性布局中添加兩個線性布局
[html]?view plaincopy
<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="horizontal"?? ????tools:context=".MainActivity"?>?? ?? ????<LinearLayout?? ????????android:id="@+id/linerlayout1"?? ????????android:layout_width="0dp"?? ????????android:layout_height="match_parent"?? ????????android:layout_weight="1"?? ????????android:background="#CCCCCC"?? ????????android:orientation="vertical"?>?? ?? ????????<Button?? ????????????android:id="@+id/button1"?? ????????????android:layout_width="match_parent"?? ????????????android:layout_height="wrap_content"?? ????????????android:text="顯示窗口"?/>?? ????</LinearLayout>?? ?? ????<LinearLayout?? ????????android:id="@+id/linerlayout2"?? ????????android:layout_width="0dp"?? ????????android:layout_height="match_parent"?? ????????android:layout_weight="3"?? ????????android:background="#CCFFDD"?? ????????android:orientation="vertical"?>?? ????</LinearLayout>?? ?? </LinearLayout>??
right.xml是等會使用fragment的時候,加載的一個布局文件:(由于主要是在界面中加載、所以不作特殊要求)
[html]?view plaincopy
<?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"?>?? ?? ????<RatingBar?? ????????android:id="@+id/ratingBar1"?? ????????android:layout_width="wrap_content"?? ????????android:layout_height="wrap_content"?/>?? ?? ????<Button?? ????????android:id="@+id/button11"?? ????????android:layout_width="wrap_content"?? ????????android:layout_height="wrap_content"?? ????????android:text="點我試試"?/>?? ?? </LinearLayout>??
MyFragment.java就是加載fragment的類,要繼承Fragment類:(要重載父類的下邊三個方法)
[java]?view plaincopy
package?com.lc.tablet_fragment_addview;?? ?? import?android.app.Fragment;?? import?android.os.Bundle;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.view.ViewGroup;?? import?android.widget.Button;?? import?android.widget.Toast;?? ?? public?class?MyFragment?extends?Fragment?{?? ?? ????public?MyFragment()?{?? ?????????? ????}?? ?? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ?????????? ????????super.onCreate(savedInstanceState);?? ????}?? ?? ????@Override?? ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?? ????????????Bundle?savedInstanceState)?{?? ?????????? ????????View?view?=?inflater.inflate(R.layout.right,?null);?? ????????Button?button?=?(Button)?view.findViewById(R.id.button11);?? ????????button.setOnClickListener(new?OnClickListener()?{?? ?? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ????????????????Toast.makeText(getActivity(),?"hello?world!",?Toast.LENGTH_LONG)?? ????????????????????????.show();?? ????????????}?? ????????});?? ????????return?view;?? ????}?? ?? ????@Override?? ????public?void?onPause()?{?? ?????????? ????????super.onPause();?? ????}?? }??
MainActivity.java:
[java]?view plaincopy
package?com.lc.tablet_fragment_addview;?? ?? import?android.app.Activity;?? import?android.app.FragmentManager;?? import?android.app.FragmentTransaction;?? import?android.os.Bundle;?? import?android.view.Menu;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.widget.Button;?? ?? public?class?MainActivity?extends?Activity?{?? ?? ????private?Button?button;?? ????private?FragmentManager?fragmentManager;??? ????private?FragmentTransaction?fragmentTransaction;??? ?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ?? ????????button?=?(Button)?this.findViewById(R.id.button1);?? ????????fragmentManager?=?getFragmentManager();?? ?? ????????button.setOnClickListener(new?OnClickListener()?{?? ?? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ????????????????fragmentTransaction?=?fragmentManager.beginTransaction();?? ????????????????MyFragment?myFragment?=?new?MyFragment();?? ?????????????????? ????????????????fragmentTransaction.add(R.id.linerlayout2,?myFragment);?? ????????????????fragmentTransaction.commit();?? ????????????}?? ????????});?? ????}?? ?? ????@Override?? ????public?boolean?onCreateOptionsMenu(Menu?menu)?{?? ?????????? ????????getMenuInflater().inflate(R.menu.main,?menu);?? ????????return?true;?? ????}?? ?? }??
演示效果:當點擊灰色界面的按鈕時顯示右側的布局:
二、第二種方法
項目結構和上圖中的差不多:只是在布局文件中,直接使用fragment控件:
[html]?view plaincopy
<RelativeLayout?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:paddingBottom="@dimen/activity_vertical_margin"?? ????android:paddingLeft="@dimen/activity_horizontal_margin"?? ????android:paddingRight="@dimen/activity_horizontal_margin"?? ????android:paddingTop="@dimen/activity_vertical_margin"?? ????tools:context=".MainActivity"?>?? ?? ????<fragment?? ????????android:id="@+id/fragment1"?? ????????android:name="com.example.tablet_fragment_fragementmanager.MyFragment"?? ????????android:layout_width="wrap_content"?? ????????android:layout_height="wrap_content"?? ????????android:layout_alignParentTop="true"?? ????????android:layout_centerHorizontal="true"?? ????????android:layout_marginTop="37dp"?/>?? ?? </RelativeLayout>??
在myfragment.java文件中,只需找到fragment所容納的布局文件即可,不進行業(yè)務上的操作:
[java]?view plaincopy
package?com.example.tablet_fragment_fragementmanager;?? ?? import?android.app.Fragment;?? import?android.os.Bundle;?? import?android.view.LayoutInflater;?? import?android.view.View;?? import?android.view.ViewGroup;?? ?? public?class?MyFragment?extends?Fragment?{?? ?? ????public?MyFragment()?{?? ????}?? ?? ????@Override?? ????public?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????}?? ?? ????@Override?? ????public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,?? ????????????Bundle?savedInstanceState)?{?? ????????? ? ?? ????????View?view?=?inflater.inflate(R.layout.text,?null);?? ????????return?view;?? ????}?? ?? ????@Override?? ????public?void?onResume()?{?? ????????super.onResume();?? ????}?? ?? }??
MainActivity.java文件:進行fragment的業(yè)務處理
[java]?view plaincopy
package?com.example.tablet_fragment_fragementmanager;?? ?? import?android.app.Activity;?? import?android.app.FragmentManager;?? import?android.os.Bundle;?? import?android.view.Menu;?? import?android.view.View;?? import?android.view.View.OnClickListener;?? import?android.widget.Button;?? import?android.widget.Toast;?? ?? ? ? ? ?? public?class?MainActivity?extends?Activity?{?? ?? ????private?MyFragment?fragment;?? ????private?FragmentManager?fragmentManager;?? ????private?Button?button;?? ?? ????@Override?? ????protected?void?onCreate(Bundle?savedInstanceState)?{?? ????????super.onCreate(savedInstanceState);?? ????????setContentView(R.layout.activity_main);?? ?? ????????fragmentManager?=?getFragmentManager();?? ?????????? ????????fragment?=?(MyFragment)?fragmentManager?? ????????????????.findFragmentById(R.id.fragment1);?? ?? ?????????? ?? ?????????? ?? ?????????? ????????button?=?(Button)?fragment.getView().findViewById(R.id.button1);?? ????????button.setOnClickListener(new?OnClickListener()?{?? ?? ????????????@Override?? ????????????public?void?onClick(View?v)?{?? ????????????????Toast.makeText(MainActivity.this,?"hello?world!",?? ????????????????????????Toast.LENGTH_SHORT).show();?? ????????????}?? ????????});?? ????}?? ?? ????@Override?? ????public?boolean?onCreateOptionsMenu(Menu?menu)?{?? ????????getMenuInflater().inflate(R.menu.main,?menu);?? ????????return?true;?? ????}?? ?? }??
總結
以上是生活随笔為你收集整理的Android学习笔记之Fragment的两种使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。