Android入门练习——Fragment+TagHost

本文介绍了Android中的Fragment和TagHost知识点。Fragment适用于大屏幕设备的布局,需要嵌套在Activity中使用,拥有自己的生命周期。文章详细讲解了如何使用FragmentManager管理和操作Fragment,包括静态和动态加载的方式。另一方面,TagHost的功能逐渐被淘汰,因其在复杂开发中的灵活性不足。最后,文章还展示了TabHost的使用,提供了一个选项卡式的用户界面效果。
摘要由CSDN通过智能技术生成

这几天学了两个可以用来布局的知识点: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);
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值