Android界面编程之Tabhost

使用TabHost的一般步骤:
(1)在界面中定义TabHost组件,为该组件定义选项卡内容
(2)Activity继承TabActivity(已经过时,现在用Fragment代替)
(3)调用TabActivity的getTabHost()方法获得TabHost对象
(4)通过TabHost对象的方法来创建添加选项卡

使用TabHost时有两种情况:一种直接把内容写在TablHost里面,一种可以写在另外的Layout中,在TabHost中引用
此次示例采用第二种方法:

示例:
这里写图片描述   这里写图片描述
首先定义三个布局文件:
tab1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tab1">
        <ViewSwitcher
            android:id="@+id/viewSwitcher1"
            android:layout_width="match_parent"
            android:layout_height="400dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:id="@+id/butten_prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="上一页"
            android:onClick="prev"/>
        <Button
            android:id="@+id/butten_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下一页"
            android:onClick="next"
            />
    </LinearLayout>

</LnearLayout>

tab2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tab2">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="send"
        android:text="发送通知"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="cancel"
        android:text="取消发送"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="openanother"
        android:text="打开另一个activity"/>
</LinearLayout>

tab3

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tab3">
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@drawable/img1"/>

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:max="255"
        android:progress="255"
        android:layout_marginLeft="0dp"/>

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.5"
        android:rating="5"/>
</LinearLayout>

MainActivity:

public class MainActivity extends TabActivity{
    //tab1
    private ViewSwitcher viewSwitcher;
    private int[] images = new int[]{R.drawable.img1,R.drawable.img2,
            R.drawable.img3,R.drawable.img4,R.drawable.img5};
    private int index = 0;
    private LayoutInflater inflater;
    //tab2
    private static final int NOTIVICATIONID=0x123;
    private NotificationManager manager;
    private Intent intent;
    //tab3

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getTabHost());
        TabHost host = getTabHost();
        LayoutInflater.from(this).inflate(R.layout.tab1,host.getTabContentView(),true);
        LayoutInflater.from(this).inflate(R.layout.tab2,host.getTabContentView(),true);
        LayoutInflater.from(this).inflate(R.layout.tab3,host.getTabContentView(),true);
        host.addTab(host.newTabSpec("tab1").setIndicator("相册").setContent(R.id.tab1));
        host.addTab(host.newTabSpec("tab2").setIndicator("发送信息").setContent(R.id.tab2));
        host.addTab(host.newTabSpec("tab3").setIndicator("透明度").setContent(R.id.tab3));
        //tab1
        viewSwitcher=(ViewSwitcher)findViewById(R.id.viewSwitcher1);
        inflater = LayoutInflater.from(this);
        viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(getApplicationContext());
                return imageView;
            }
        });
        viewSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
        viewSwitcher.setOutAnimation(this,android.R.anim.slide_out_right);
        next(null);
        //tab2
        manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        //tab3
        final ImageView imageView = (ImageView)findViewById(R.id.imageView2);
        final SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
        final RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                imageView.setImageAlpha(i);
                ratingBar.setRating(i*5.0f/255);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                imageView.setImageAlpha((int)v*255/5);
                seekBar.setProgress((int)v*255/5);
            }
        });
    }
    //tab1
    public void prev(View view){
        index--;
        if (index<0)
            index=images.length-1;
        ImageView imageView = (ImageView)viewSwitcher.getNextView();
        imageView.setImageResource(images[index]);
        viewSwitcher.showPrevious();
    }
    public void next(View view){
        index++;
        if (index>images.length-1)
            index=0;
        ImageView imageView = (ImageView)viewSwitcher.getNextView();
        imageView.setImageResource(images[index]);
        viewSwitcher.showNext();
    }
    //tab2
    public void send(View view){
        intent = new Intent(this,MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),0,intent,0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setAutoCancel(true)
                .setTicker("中午加餐啦")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("今天牛老板请客")
                .setContentText("牛老板请客吃烧烤")
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentIntent(pi)
                .build();
        Notification notify = builder.build();
        manager.notify(NOTIVICATIONID,notify);
    }
    public  void  cancel(View view){
        manager.cancel(NOTIVICATIONID);
    }
    public void openanother(View view){
        startActivity(intent);
    }
}

猜你喜欢

转载自blog.csdn.net/feiqinbushizheng/article/details/78868519