一、fragment
Api11中11中引入。标签页,被嵌入到activity中,在ViewGroup中,fragment是作为注意小写
【1】入门
1)在Activity中添加布局
(a)在layout中声明:不常用
<xml>
<LinearLayout>
<fragment android:name="自定义的Fragment"></fragment>
<fragment android:name="自定义的Fragment"></fragment>
</LinearLayout>
(b)在Activity中动态添加Fragment,常用
//获取fragmentManager
FragmentManager fm = getFragmentManager();
//开启事物
FragmentTransation ft = fm.beginTransation();//开启事物
//加载fragment
//加载一个fragment,android.R.id.content//当前手机的窗体,系统定义
ft.replace(containerViewId,fragment);//窗体视图的ID,要加载的fragment
//提交事物
ft.commit();
2)创建一个Fragment
public class myFragment extends Fragment{
//加载布局,和Activit中的setContentView(R.layout.myView)功能一样;
//当用户第一次画界面时调用
@override
public View onCreateView(LayoutInflater,ViewGroup,Bandle){
//加载布局
return layoutInflater.inflate(R.layout.mylayout,ViewGroup,false);
}
}
2)fragment中的点击事件不可以在布局中定义onclick属性
【2】版本兼容
这个控件是在API11中引入的,如果需要兼容低版本的话,需要引入android-suppert-v4.jar中的Fragment类,相关的类也要引入V4包中的。获取FragmentManager需要是用获取支持包的中方法
getsupportFragmentManager();
【3】生命周期
onAttach() 依附
onDetach() 分离
onCreateView(); //加载view
onDestroyView();//加载的view销毁
onActivityCreated(); 在onCreateView()中准备的View都初始化完毕
一般使用的时候重写两个方法就可以了,onCreateView()和onDestroy();就可以了
【4】fragment之间的通信
1)Activity中定义Fragment
Activity中开始的fragment的事物中使用带tag的方法来实现;
FragmentTranction tf = FragmentManager.beginTranction();
tf.replace(R.id.layoutid,new Fragment(),"tag")//
2)Fragment获取Activity
fragment 获取activity getAcitvity();通过Activity来获取FragmentManager(),调用findFragmentByTag();来获取activity中定义的fragment.
getActivity().getFragmentManager().findFragmentByTag("trg");
3)说明
所有的Fragment都是放在Activity中,所以他们之间的通信需要通过桥梁activity来实现。
二、TabHost 标签页
【1】布局设计
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/tabs"/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/page1"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个页面">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/page2"
>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个页面">
【2】代码实现
1)//获取定义的标签
TabHost tabhost = findViewById(R.id.tabhost);
2)/在调用setup是,系统会获取TabWidget标签和内容FrameLayout,这两个控件的id也是系统定义好的,上面有做解释
tabhost.setup();
3)添加标签页
//添加一个标签页
TabHost.TabSpec tabSpec = tabhost.newTabSpec("first"); //标签页的唯一标识
//文字和和图片,也可以接收一个自定义的View对象
tabSpec.setIndicator("首页",getResources().getDrawable(R.drawable.ic_launcher_background));
//指定界面的ID
tabSpec.setContent(R.id.page1);
tabhost.addTab(tabSpec);
4)设置默认显示的页面
默认显示第一个页面
tabhost.setCurrentTab(0);
5)自定义标签
View view = getLayoutInflater().inflate(R.layout.activity_tabhost,null);
TextView tv = view.findViewById(R.id.atv_tips);
tv.setText("首页");
tabSpec.setIndicator(view);
三、menu
带单按钮,对应的是硬件中的带单按钮,目前手机基本废除了这个按钮所以此功能使用的已经很少了
1)在创建MainActivity时,系统初始化OnCreateOptionsMenu(Menu menu);方法来初始化菜单。
getMenuInflater.inflate(R.menu.main,menu);
2)菜单的展示放在资源目录menu中
……
3)点击事件,重写onOptionsItemSelected(MenuItem item) 方法
4)动态添加menu,在onCreateOptionsMenu(Menu menu)中动态添加
// int 分组,int菜单Id,int排序,String题目
menu.add(groupid,itemId,order,title);
5)打开前的方法
类似按后退键,调用的onBackPressed();按后退键之后,menu也有一个按菜单前的事件onMenuOpened();按下菜单键打开菜单后。注释掉super的父类方法,重新弹出效果。
事件仅仅绑定在当前的Activity
四、AutoCompleteTextView控件
搜索框的内容提示,和listView显示的效果类似原理一样。
1)定义声明布局
android:id="@+id/atv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
2)数据显示,原理和ListView一样,需要适配器Adapter
AutoCompleteTextView actv = findById(R.id.atv_tip);
ArrayAdapter aa = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,new Object[]{"12",2,"3"});
actv.setAdapter(aa);
3)默认提示效果是两个字符,如果需要修改这个控件的属性
android:completionThreshold="1"