这几天学了两个可以用来布局的知识点:TagHost 和 Fragment, 其中TagHost中有些功能已经被淘汰了,因为涉及到比较大的开发时很不灵活;而Fragment是现在比较常用的布局方式。
Fragment
Fragment是Android3.0以后引入的,就是用来给大屏幕(例如平板电脑)布局用的,用它可以把屏幕分割成不同的模块,然后各个模块调用activity时可以更方便。
Fragment不能单独使用它需要嵌套在Activity中使用,它有自己的生命周期,但是还是会受到它的宿主Activity的声明周期影响,宿主Activity销毁,那么该fragment也会消失。
管理Fragment
使用FragmentManager来管理Activity中的fragments,可以通过调用getFragmentManager()获取FragmentManager。
使用Fragment时,可以通过用户交互来执行一些动作,比如增加,移除、替换等等。所有这些改变构成一个集合,这个集合被叫做一个transaction。
每个transaction是一组同时执行的变化的集合。用add(),remove(),replace()方法把所有需要的变化加进去,然后调用commit()方法,将这些变化应用。
可以通过静态或动态加载Fragment:
静态加载:
方法:
1 在activity_main.xml中写两个Fragment
标签,方便之后将两个标签加载到主页面上。
<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="vertical" >
<fragment
android:id="@+id/f1"
android:name="com.example.fragmentdemostatic.Fragment1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<fragment
android:id="@+id/f2"
android:name="com.example.fragmentdemostatic.Fragment2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
注意:必须给两个fragment加上id不然加载不出来。
2 分别创建两个xml布局文件,命名为fragment1和fragment2,将背景设置成不同的颜色,方便区分。
例如:
其中fragment1设置成:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#7fa99b"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is fragment 1"
android:textSize="25sp"/>
</LinearLayout>
3 声明两个class 分别用来加载fragment1和2
其中Fragment1.java:
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 使用inflate对象 将xml布局文件填充为view对象
//return super.onCreateView(inflater, container, savedInstanceState);
//inflate参数好哒含义
//1 布局资源的id
//2 填充的根视图
//3 是否将载入的视图绑定到根视图
return inflater.inflate(R.layout.fragment1, container, false);
//View vv=inflater.inflate(R.layout.fragment1, container, false);
//return vv;
}
}
4 在MainActivity.java中将两个类分别实例化以后放到layout_linear.xml中
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
效果图:
动态加载:
其与静态加载的区别就是,不在主页面activity_main.xml中写两个fragment,二是到Mainactivity.java里面加载。(注意,此时的activity_main.xml里面也要写id了,如:android:id="@+id/layout_linear"
)
比如,需要实现屏幕旋转效果时,可将代码设置如下:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//display提供了屏幕大小和密度等信息
Display dis=getWindowManager().getDefaultDisplay();
// if(dis.getWidth()>dis.getHeight()){}过时了
Point size=new Point();
dis.getSize(size);
if(size.x>size.y)//x=width y=height
{
Fragment1 frg1=new Fragment1();
this.getFragmentManager().beginTransaction().replace(R.id.layout_linear, frg1);//替换 并加载到activity中
//replace也可以是 add
}else{
Fragment2 frg2=new Fragment2();
this.getFragmentManager().beginTransaction().replace(R.id.layout_linear, frg2);//替换 并加载到activity中
}
}
}
(此处缺张效果图,我不知道模拟机里面咋条旋转。。。)
TabHost 选项卡
显而易见,tabhost就是选项卡。
效果:
activity_main中:
<TabHost 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:id="@android:id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<LinearLayout
android:id="@+id/content1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是选项卡1的内容"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/content2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是选项卡2的内容"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/content3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是选项卡3的内容"
/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
MainActivity.java中:
public class MainActivity extends ~~TabActivity~~ {//已淘汰
private TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost=this.getTabHost();
TabSpec page1=tabHost.newTabSpec("tab1")//创建新选项卡
.setIndicator("标签1")//创建标签内容
.setContent(R.id.content1);
tabHost.addTab(page1);
TabSpec page2=tabHost.newTabSpec("tab1")
.setIndicator("标签2")
.setContent(R.id.content2);
tabHost.addTab(page2);
TabSpec page3=tabHost.newTabSpec("tab1")
.setIndicator("标签3")
.setContent(R.id.content3);
tabHost.addTab(page3);
}
}