星空网 > 软件开发 > 操作系统

Android Fragment初探:静态Fragment组成Activity

一直习惯了在Activity中写所有事件处理代码,直到认真学习Fragment时,才发现,Activity完全可以由多个Fragment组成。

对Fragment的了解还不够深入,先从静态Fragment开始练习,把Fragment当成单纯的Activity控件。

就来写一个最常用的“标题+正文”布局吧!

 

activity_main.

 1 <RelativeLayout ="http://schemas.android.com/apk/res/android" 2         ="http://schemas.android.com/tools" 3         android:layout_width="match_parent" 4         android:layout_height="match_parent" 5         tools:context=".MainActivity"> 6  7   <fragment 8     android:id="@+id/fragment_title" 9     android:name="com.example.hopecapital.myapplication.TitleFragment"10     android:layout_width="fill_parent"11     android:layout_height="45dp" />12 13   <fragment14     android:layout_below="@id/fragment_title"15     android:id="@+id/id_fragment_content"16     android:name="com.example.hopecapital.myapplication.ContentFragment"17     android:layout_width="fill_parent"18     android:layout_height="fill_parent" />19 20 </RelativeLayout>

主界面的布局,需要写上用到的fragment,并规定好他们的位置。

 

MainActivity.java:

 1 package com.example.hopecapital.myapplication; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5  6  7 public class MainActivity extends Activity { 8  9   @Override10   protected void onCreate(Bundle savedInstanceState) {11     super.onCreate(savedInstanceState);12     setContentView(R.layout.activity_main);13   }14 15 }

这里只需要加载布局即可。

 

接下来是两个fragment的布局和java代码。

fragment_title.

 1 <??> 2 <RelativeLayout ="http://schemas.android.com/apk/res/android" 3         android:layout_width="match_parent" 4         android:layout_height="45dp"> 5  6   <ImageButton 7     android:id="@+id/btn_menu" 8     android:layout_width="wrap_content" 9     android:layout_height="wrap_content"10     android:layout_centerVertical="true"11     android:layout_marginLeft="3dp"12     android:background="@mipmap/ic_launcher" />13 14   <TextView15     android:layout_width="fill_parent"16     android:layout_height="fill_parent"17     android:gravity="center"18     android:text="标题"19     android:textSize="20sp"20     android:textStyle="bold" />21 22 </RelativeLayout>

 

TitleFragment.java:

 1 package com.example.hopecapital.myapplication; 2  3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.ImageButton; 9 import android.widget.Toast;10 11 public class TitleFragment extends Fragment {12   private ImageButton Menu;13 14   @Override15   public View onCreateView(LayoutInflater inflater, ViewGroup container,16                Bundle savedInstanceState){17     View view = inflater.inflate(R.layout.fragment_title,container,false);18     Menu = (ImageButton)view.findViewById(R.id.btn_menu);19 20     Menu.setOnClickListener(new View.OnClickListener() {21       @Override22       public void onClick(View v) {23 24         Toast.makeText(getActivity(),"titleFragment!",Toast.LENGTH_SHORT).show();25       }26     });27     return view;28   }29 }

 

fragment_content.

 1 <??> 2 <RelativeLayout ="http://schemas.android.com/apk/res/android" 3         android:layout_width="match_parent" 4         android:layout_height="match_parent"> 5  6   <TextView 7     android:layout_width="match_parent" 8     android:layout_height="match_parent" 9     android:gravity="center"10     android:text="正文区域"11     android:textSize="20sp" />12 13 </RelativeLayout>

 

ContentFragment.java:

 1 package com.example.hopecapital.myapplication; 2  3  4 import android.app.Fragment; 5 import android.os.Bundle; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 11 /**12  * A simple {@link Fragment} subclass.13 */14 public class ContentFragment extends Fragment {15 16 17   public ContentFragment() {18     // Required empty public constructor19   }20 21 22   @Override23   public View onCreateView(LayoutInflater inflater, ViewGroup container,24                Bundle savedInstanceState) {25     return inflater.inflate(R.layout.fragment_content,container,false);26   }27 28 29 }

 

至此,Activity由两部分Fragment分别控制,需要改哪里直接找对应Fragment即可。




原标题:Android Fragment初探:静态Fragment组成Activity

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Prime会员不再包邮?亚马逊打了什么算盘:https://www.ikjzd.com/articles/149056
(精品分析)变调夹类目亚马逊美国站市场调查选品报告:https://www.ikjzd.com/articles/14906
增长率81%的墨西哥电商市场 你准备好了吗?:https://www.ikjzd.com/articles/149061
单箱净利润14元,内贸箱港口发展如何破局?:https://www.ikjzd.com/articles/149067
巴西消费者机构对Shopee上架产品提出新需求:https://www.ikjzd.com/articles/149070
新加坡物流独角兽 获阿里巴巴等机构5.78亿美元融资:https://www.ikjzd.com/articles/149071
珠海长隆横琴剧院票价是多少:https://www.vstour.cn/a/335166.html
珠海长隆横琴剧院交通指引:https://www.vstour.cn/a/335167.html
相关文章
我的浏览记录
最新相关资讯
跨境电商服务平台 | 深圳旅行社 | 东南亚物流