Anroid-通用组件- 滚动视图组件(ScrollView)-选项卡组件(TabHost-TabWidge-FrameLayout)

Android提供了ScrollView滚动视图组件,用于为组件提供滚动效果,还提供了选项卡组件它由(TabHost-TabWidge-FrameLayout)三个组件组成,

滚动视图组件(ScrollView)

1.当内容比较多,不能一屏显示的时候,就用到了滚动条组件,用户可以通过滚动屏幕查看完整内容。
2.滚动视图组件继承自帧布局管理器,
3.因此在滚动条组件中可以添加任意组件,但是一个滚动视图里只能放一个组件。
4.如果有放入多个组件组件的需求,可以在滚动视图里添加一个布局管理器,然后将组件放在布局管理器中
5.滚动条拖动才会显示。默认不显示,停止拖动后,会消失

1基本语法

<ScrollView 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">
  <布局管理器>
   	 <组件>
 <布局管理器>
</ScrollView>    

2.实例利用java代码添加滚动条(不是直接在布局上加)----此处有直接在java代码中创建组件的方法

①在string.xml文件里设置一个文本数据源shi_text

 <string name="shi_text">
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
        			   2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       2\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
                       一\t\t\t\t\t\t万\n\n
    </string>

②xml文件,设置一个布局管理器,并设置id和布局方式

<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"
    tools:context="${relativePackage}.${activityClass}"
    android:orientation="vertical"
    android:id="@+id/scrollview_layout"                     `设置布局id`
    android:gravity="center_horizontal"                     `设置布局方式`
     >
</LinearLayout>

③java文件

原理:
Ⅰ.获取布局管理器组件,并创建文本组件和滚动条组件,
Ⅱ.把文本数据添加到文本组件里(.setText()),再把文本组件添加到滚动条组件里(.addView),在把滚动条组件添加到布局管理器中(.addView

public class ScrollViewActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scroll_view);
		
		`根据id获取线性布局管理器`
		LinearLayout scrollview_layout=(LinearLayout) findViewById(R.id.scrollview_layout);
		`创建滚动条组件`
		ScrollView scrollview = new ScrollView(ScrollViewActivity.this);   `参数:this`
		`创建文本框组件`
		TextView textview = new TextView(ScrollViewActivity.this);         `参数:this`
		textview.setText(R.string.shi_text);        `添加文本内容到文本框组件`
		scrollview.addView(textview);				`把文本框添加到滚动视图组件`
		scrollview_layout.addView(scrollview);      `将滚动视图组件添加带布局文件`
	}
}

效果:::
在这里插入图片描述

选项卡组件

1.当一个页面无法满足显示需求,
2.同时这些页面具有不同的功能属性,
3.则可以采用选项卡组件,这样既满足分页显示还不至于排列凌乱。

1.实现选项卡分页功能的步骤

1.在布局文件中添加实现选项卡的三个组件即,TabHost-TabWidge-TabContent.通常情况下TabContent组件需要FrameLayout来实现
2.为不同的分页建立对应的Xml布局文件
3.在Activity中获取并初始化TabHost组件
4.为TabHost对象添加标签页

2.TabHost-TabWidge-TabContent-TabHost详解

  1. 使用选项卡组件,TabHost,TabHost必不可少
  2. TabWidget : 该组件就是TabHost标签页中上部 或者 下部的按钮, 可以点击按钮切换选项卡;默认按钮在上边
    将按钮放到下面:将该组件定义在下面, 但是注意,FrameLayout要设置android:layout_widget = 1;
    设置TabWidget大小 : 如果想要设置该按钮组件的大小, 可以设置该组件与FrameLayout组件的权重;
  3. Fragement组件代表内容
  4. TabContent.通常情况下TabContent组件需要FrameLayout来实现

在这里插入图片描述

3.实例—切换xml页面—问题点击切换的页面(tabhost_first.xml)只能是下边的格式,别的加载不出来!!!!

①创建选项卡的切换文件

tabhost_first.xml

<?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"
    android:id="@+id/tabhost_first" 
    >
    
    <ImageView
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:src="@drawable/a3"
        />
</LinearLayout>

tabhost_second.xml

<?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"
    android:id="@+id/tabhost_second"
    >
    
    <ImageView
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:src="@drawable/a4"
        />

</LinearLayout>

tabhost_three.xml

<?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"
    android:id="@+id/tabhost_three"
    >
    
    <ImageView
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:src="@drawable/a5"
        />

</LinearLayout>

②主要的xml文件----tabhost.xml

<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"
    tools:context="${relativePackage}.${activityClass}"
    android:id="@android:id/tabhost"
     >
     <!-- TabHost组件的命名方式不一样,不按照这个命名会报错 -->
     <!-- 设置id是用的@android:id/ -->
    <LinearLayout
        android:layout_width="match_parent"
    	android:layout_height="match_parent" 
    	android:orientation="vertical"
        > 
        `FrameLayout`       
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent" 
            `下部显示文字`
            android:layout_weight="1"
            >  
        </FrameLayout>
        `TabWidget`
        <TabWidget
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:id="@android:id/tabs" >              
        </TabWidget>
    </LinearLayout>
</TabHost>

③java文件

原理:获取选项卡组件,实例化LayoutInflater对象,利用inflate方法放入把页面放入选项卡内,实例化切换页面。设置文本与页面相对应

public class TabHostActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_host);
		
		`获取选项卡组件`        	                
		TabHost tabhost = (TabHost) findViewById(`android`.R.id.tabhost);
		`加载选项卡组件`
		tabhost.setup();
		`声明并实例化一个LayoutInflater对象`
		LayoutInflater layoutinflater = LayoutInflater.from(this);     `注意,没有new`
		`调用inflate方法        参数1:引入页面            参数2:把页面放进选项卡`
		layoutinflater.inflate(R.layout.tabhost_first, tabhost.getTabContentView());
		layoutinflater.inflate(R.layout.tabhost_second,tabhost.getTabContentView());
		layoutinflater.inflate(R.layout.tabhost_three,tabhost.getTabContentView());
		
		
		`实例化页面                         参数:标签名       参数:按钮文本      图片              参数:放入页面 `   
		tabhost.addTab(tabhost.newTabSpec("tabhost_first").setIndicator("花",null).setContent(R.id.tabhost_first));
		`实例化页面                                                            引入图片的方法`
		tabhost.addTab(tabhost.newTabSpec("tabhost_second").setIndicator("水母"getResources().getDrawable(R.drawable.b2)).setContent(R.id.tabhost_second));
		`实例化页面`
		tabhost.addTab(tabhost.newTabSpec("tabhost_three").setIndicator("水母").setContent(R.id.tabhost_three));
		
	}
}

效果:::

在这里插入图片描述

实例3,切换Activity活动

原理:同上,继承TabActivity父类,不需要实例化页面,需要引入Intent对象,设置文字与Activity对应

									 `继承TabActivity父类-划线表示过时`
public class TabHostActivity extends TabActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_host);
		`设置传递信使`
		Intent intent1 = new Intent(this,CheckBoxActivity.class);
		Intent intent2 = new Intent(this,SpinnerActivity.class);
		Intent intent3 = new Intent(this,SeekBarActivity.class);
		
		`获取选项卡组件`        	                 `android.R.id.tabhost`
		TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
		`加载`
		tabhost.setup();
	
		`声明并实例化一个LayoutInflater对象`
		LayoutInflater layoutinflater = LayoutInflater.from(this);     `注意,没有new`
		//添加第一个标签                             `标签名`                                                                                                               
				tabhost.addTab(tabhost.newTabSpec("tabhost_first")
						            `按钮名  显示的图片`    
						.setIndicator("花",getResources().getDrawable(R.drawable.b0))
						`引入文件`
						.setContent(intent1)
						);
		//添加第二个标签
				tabhost.addTab(tabhost.newTabSpec("tabhost_second")
						.setIndicator("水母",null)
						.setContent(intent2)
						);
				
				
		//添加第三个标签
				tabhost.addTab(tabhost.newTabSpec("tabhost_three")
						.setIndicator("水母",null)
						.setContent(intent3)
						); 		
	}
}

效果:::

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值