进度条组件
进度条
进度条使用ProgressBar标签来使用
进度条有多个类型
- 水平进度条 - 显示实时进度
- 圆形进度条 - 正在加载
style属性
通过android主题属性设置进度条
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:max
设置进度最大值
android:progress
进度条开始时的进度
更新进度条的进度
进度条的操作是一个耗时操作,需要开启一个线程,获取任务的进度,来更新进度条
实例:
创建一个进度条组件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@mipmap/xxll"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.MainActivity">
<!-- 水平进度条 -->
<ProgressBar
android:id="@+id/progressBar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="60dp"
android:max="100" />
</RelativeLayout>
模拟进度条组件的更新
private ProgressBar horizonP; //水平进度条
private int mProgressStatus = 0; //完成进度
private Handler mHandler; //声明一个用于处理消息的Handler类的对象
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //设置全屏显示
horizonP = (ProgressBar) findViewById(R.id.progressBar1); //获取水平进度条
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x111) {
horizonP.setProgress(mProgressStatus); //更新进度
} else {
Toast.makeText(MainActivity.this, "耗时操作已经完成", Toast.LENGTH_SHORT).show();
horizonP.setVisibility(View.GONE); //设置进度条不显示,并且不占用空间
}
}
};
new Thread(new Runnable() {
public void run() {
while (true) {
mProgressStatus = doWork(); //获取耗时操作完成的百分比
Message m = new Message();
if (mProgressStatus < 100) {
m.what = 0x111;
mHandler.sendMessage(m); //发送信息
} else {
m.what = 0x110;
mHandler.sendMessage(m); //发送消息
break;
}
}
}
//模拟一个耗时操作
private int doWork() {
mProgressStatus += Math.random() * 10; //改变完成进度
try {
Thread.sleep(200); //线程休眠200毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
return mProgressStatus; //返回新的进度
}
}).start(); //开启一个线程
拖动条
使用SeekBar创建一个拖动条
SeekBar时进度条的一个子类,所以可以设置最大进度android:max和当前进度android:progress
可以把拖动点设置为一个图片
android:thumb 设置组图资源就行
实例:通过拖动条设置图片的透明度
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.MainActivity">
<!-- 设置一张山水图片-->
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@mipmap/lijiang"/>
<!-- 定义一个拖动条 -->
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255"
/>
<!--属性图片 -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@mipmap/meitu"/>
</LinearLayout>
package com.mingrisoft;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private ImageView image; //定义图片
private SeekBar seekBar; //定义拖动条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image); //获取图片
seekBar = (SeekBar) findViewById(R.id.seekbar); //获取拖动条
//为拖动条设置监听事件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
// 当拖动条的滑块位置发生改变时触发该方法
@Override
public void onProgressChanged(SeekBar arg0, int progress,
boolean fromUser) {
// 动态改变图片的透明度
image.setImageAlpha(progress);
}
@Override
public void onStartTrackingTouch(SeekBar bar) {
}
@Override
public void onStopTrackingTouch(SeekBar bar) {
}
});
}
}
星级评分条
使用RatingBar创建星级评分条
android:numStars
设置星星的数量,星星过多,星星可能显示不下
android:rating
设置星星的起始值
android:stepSize
指定每次最少需要改变多少个星星,默认时0.5
android:isIndicator
设置为true,星星就不能改变,只能看
实例:
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="0"
android:layout_above="@+id/btn"
android:layout_marginBottom="60dp"/>
public class MainActivity extends Activity {
private RatingBar ratingbar; //星级评分条
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingbar = (RatingBar) findViewById(R.id.ratingBar1); //获取星级评分条
Button button=(Button)findViewById(R.id.btn); //获取“提交”按钮
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = ratingbar.getProgress(); //获取进度
float rating = ratingbar.getRating(); //获取等级
float step = ratingbar.getStepSize(); //获取每次最少要改变多少个星级
Log.i("星级评分条","step="+step+" result="+result+" rating="+rating);
Toast.makeText(MainActivity.this, "你得到了" + rating + "颗星", Toast.LENGTH_SHORT).show();
}
});
}
}
图形类组件
图像视图
使用ImageView创建图像视图
android:src
指定要显示的图片
android:src="@mipmap/flower"
对图像视图进行缩放-等比例
android:layout_height="90dp"
android:layout_width="90dp"
android:scaleType - 修改缩放方式
fitXY 等比例缩放,可能会变形
android:adjustViewBounds
设置图片的最大高度、最大宽度
android:maxWidth="90dp"
android:maxHeight="90dp"
android:adjustViewBounds="true"
android:tint
为图像着色 - 设置成半透明等等
android:tint="#77ff0000"
图像切换器
带动画效果的图片切换功能
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.MainActivity">
<!--图像切换器-->
<ImageSwitcher
android:id="@+id/imageswitcher"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageSwitcher>
</RelativeLayout>
package com.mingrisoft;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class MainActivity extends Activity {
private int[] arrayPictures = new int[]{R.mipmap.img01, R.mipmap.img02, R.mipmap.img03,
R.mipmap.img04, R.mipmap.img05, R.mipmap.img06,
R.mipmap.img07, R.mipmap.img08, R.mipmap.img09,
};// 声明并初始化一个保存要显示图像ID的数组
private ImageSwitcher imageSwitcher; // 声明一个图像切换器对象
//要显示的图片在图片数组中的Index
private int pictutureIndex;
//左右滑动时手指按下的X坐标
private float touchDownX;
//左右滑动时手指松开的X坐标
private float touchUpX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher); // 获取图像切换器
//为ImageSwicher设置Factory,用来为ImageSwicher制造ImageView
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() { // 为图像切换器指定图像
ImageView imageView = new ImageView(MainActivity.this); // 实例化一个ImageView类的对象
imageView.setImageResource(arrayPictures[pictutureIndex]);//根据id加载默认显示图片
return imageView; // 返回imageView对象
}
});
imageSwitcher.setOnTouchListener(new View.OnTouchListener() { // 也有onClick方法,这是没有实现
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//取得左右滑动时手指按下的X坐标
touchDownX = event.getX();
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//取得左右滑动时手指松开的X坐标
touchUpX = event.getX();
//从左往右,看下一张
if (touchUpX - touchDownX > 100) {
//取得当前要看的图片的index
pictutureIndex = pictutureIndex == 0 ? arrayPictures.length - 1 : pictutureIndex - 1;
//设置图片切换的动画
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_out_right));
//设置当前要看的图片
imageSwitcher.setImageResource(arrayPictures[pictutureIndex]);
//从右往左,看上一张
} else if (touchDownX - touchUpX > 100) {
//取得当前要看的图片index
pictutureIndex = pictutureIndex == arrayPictures.length - 1 ? 0 : pictutureIndex + 1;
//设置切换动画
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_out_left));
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_in_right));
//设置要看的图片
imageSwitcher.setImageResource(arrayPictures[pictutureIndex]);
}
return true;
}
return false;
}
});
}
}
网格视图
通过行列的方式显示图片
GridView组件创建网格视图
通过适配器来来显示网格视图的图片
ArrayAdapter
SimpleAdapter
SimpleCursorAdapter
BaseAdapter
android:numColumns
指定列数
通过SimpleAdapter使用网络视图
1、创建一个网络视图
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp">
2、使用SimpleAdapter使,需要一个布局文件显示效果cell.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" >
<ImageView
android:layout_width="100dp"
android:layout_height="75dp"
android:id="@+id/image" />
</LinearLayout>
3、java代码
public class MainActivity extends AppCompatActivity {
private int[] pricture = new int[]{
R.drawable.img01,,,,,,,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridView);
List<Map<String,Object>> listitem = new ArrayList<Map<String,Object>>();
for(int i = 0; i< pricture.length; i++){
Map<String,Object> map = new HashMap<String, Object>();
map.put("image", pricture[i]);
listitem.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,
listitem,
R.layout.cell,
new String[]{"image"},
new int[]{R.id.image});
gridView.setAdapter(simpleAdapter);
}
}
通过BaseAdapter使用网络视图
1、
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.mingrisoft.MainActivity">
<!--标题栏-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/qqxiang" />
<!--年月日-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="2016年1月19号" />
<!--网格布局-->
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit" <!--自动排列-->
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" <!--垂直间距-->
/>
</GridView>
</LinearLayout>
2、
package com.mingrisoft;
import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends Activity {
//显示的图片数组
private Integer[] picture = {R.mipmap.img01, R.mipmap.img02, R.mipmap.img03,
R.mipmap.img04, R.mipmap.img05, R.mipmap.img06, R.mipmap.img07,
R.mipmap.img08, R.mipmap.img09, R.mipmap.img10, R.mipmap.img11,
R.mipmap.img12,};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView= (GridView) findViewById(R.id.gridView); //获取布局文件中的GridView组件
gridView.setAdapter(new ImageAdapter(this)); //调用ImageAdapter
}
//创建ImageAdapter
public class ImageAdapter extends BaseAdapter{
private Context mContext; //获取上下文
public ImageAdapter(Context c){
mContext=c;
}
@Override
public int getCount() {
return picture.length;//图片数组的长度
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null){ //判断传过来的值是否为空
imageView=new ImageView(mContext); //创建ImageView组件
imageView.setLayoutParams(new GridView.LayoutParams(100, 90)); //为组件设置宽高
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); //选择图片铺设方式
}else{
imageView= (ImageView) convertView;
}
imageView.setImageResource(picture[position]); //将获取图片放到ImageView组件中
return imageView; //返回ImageView
}
}
}
列表类组件
下拉列表框
Spinner创建下来列表框
android:entries
在xml中设置下拉列表框
需要创建一个数组资源文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>全部</item>
<item>电影/电视</item>
<item>图书</item>
<item>唱片</item>
<item>小事</item>
<item>用户</item>
<item>小组</item>
<item>群聊</item>
<item>游戏/应用</item>
<item>活动</item>
</string-array>
</resources>
设置
android:entries="@array/ctype"
选择下拉列表的内容
package com.mingrisoft;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner); //获取下拉列表
/****************通过指定适配器的方式为选择列表框指定列表项********************/
// 方法一
// String[] ctype=new String[]{"全部","电影","图书","唱片","小事","用户","小组","群聊","游戏","活动"}
// ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ctype);
// 方法二
// ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
// this, R.array.ctype,android.R.layout.simple_dropdown_item_1line); //创建一个适配器
//
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 为适配器设置列表框下拉时的选项样式
// spinner.setAdapter(adapter); // 将适配器与选择列表框关联
/***************************************************************************/
//为下拉列表创建监听事件
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String result = parent.getItemAtPosition(position).toString(); //获取选择项的值
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show(); //显示被选中的值
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
列表视图
通过ListView添加列表视图
也可以通过数组资源文件创建列表视图
1、在主layout中创建ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mingrisoft.MainActivity">
<!-- 标题栏-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/wei_top"
/>
<!-- 列表视图-->
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="370dp">
</ListView>
<!--下标题选择框-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/wei_down"/>
</LinearLayout>
2、创建ListView每一个条目显示样式 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 存放头像-->
<ImageView
android:id="@+id/image"
android:paddingRight="10dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:adjustViewBounds="true"
android:maxWidth="72dp"
android:maxHeight="72dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<!-- 存放名字-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center"
android:id="@+id/title"
/>
</LinearLayout>
3、java层通过适配器设置列表项
package com.mingrisoft;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.listview); // 获取列表视图
int[] imageId = new int[]{R.mipmap.img01, R.mipmap.img02, R.mipmap.img03,
R.mipmap.img04, R.mipmap.img05, R.mipmap.img06,
R.mipmap.img07, R.mipmap.img08, R.mipmap.img09,
}; // 定义并初始化保存图片id的数组
String[] title = new String[]{"刘一", "陈二", "张三", "李四", "王五",
"赵六", "孙七", "周八", "吴九"}; // 定义并初始化保存列表项文字的数组
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); // 创建一个list集合
// 通过for循环将图片id和列表项文字放到Map中,并添加到list集合中
for (int i = 0; i < imageId.length; i++) {
Map<String, Object> map = new HashMap<String, Object>(); // 实例化Map对象
map.put("image", imageId[i]);
map.put("名字", title[i]);
listItems.add(map); // 将map对象添加到List集合中
}
SimpleAdapter adapter = new SimpleAdapter(this, listItems,
R.layout.main, new String[] { "名字", "image" }, new int[] {
R.id.title, R.id.image }); // 创建SimpleAdapter
listview.setAdapter(adapter); // 将适配器与ListView关联
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String, Object> map = ( Map<String, Object> )parent.getItemAtPosition(position); //获取选择项的值
Toast.makeText(MainActivity.this,map.get("名字").toString(),Toast.LENGTH_SHORT).show();
}
});
}
}
滚动视图
当页面的内容一页显示不下的时候,通过滚动视图显示内容
1、直接xml中通过ScrollView添加滚动视图
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123"/>
<Button
android:layout_width="match_parent"
android:text="123"
android:layout_height="wrap_content" />
</ScrollView>
2、在代码中添加滚动视图
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout, linearLayout2;//定义linearLayout为默认布局管理器,linearLayout2为新建布局管理器
ScrollView scrollView;//定义滚动视图组件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.ll);//获取布局管理器
linearLayout2 = new LinearLayout(MainActivity.this);//创建一个新的布局管理器
linearLayout2.setOrientation(LinearLayout.VERTICAL);//设置为纵向排列
scrollView = new ScrollView(MainActivity.this);//创建滚动视图组件
linearLayout.addView(scrollView);//默认布局中添加滚动视图组件
scrollView.addView(linearLayout2);//滚动视图组件中添加新建布局
ImageView imageView = new ImageView(MainActivity.this);//创建ImageView组件
imageView.setImageResource(R.mipmap.cidian);//ImagView添加图片
TextView textView = new TextView(MainActivity.this);//创建TextView组件
textView.setText(R.string.cidian);//TextView添加文字
linearLayout2.addView(imageView);//新建布局中添加ImageView组件
linearLayout2.addView(textView);//新建布局中添加TextView组件
}
}
通用组件
选项卡
实现多标签页的界面
实现步骤:
1、在布局文件中添加TabHost、TabWidget和TabContent组件
2、编写各标签页的XML布局文件
3、获取并初始化TabHost组件
4、为TabHost对象添加标签页
1、
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mingrisoft.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</TabHost>
2、
tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/biaoqing_left"/>
</LinearLayout>
tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/biaoqing_right"/>
</LinearLayout>
</FrameLayout>
3-4
package com.mingrisoft;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
private TabHost tabHost;//声明TabHost组件的对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost) findViewById(android.R.id.tabhost);//获取TabHost对象
tabHost.setup();//初始化TabHost组件
LayoutInflater inflater = LayoutInflater.from(this); // 声明并实例化一个LayoutInflater对象
inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
inflater.inflate(R.layout.tab2,tabHost.getTabContentView());
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("精选表情")
.setContent(R.id.linearlayout1));//添加第一个标签页
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("投稿表情")
.setContent(R.id.framelayout));//添加第二个标签页
}
}