android FragmentTabHost的简单使用

在一篇博客里看到一个demo,多多少少有点问题,就拿来自己修改了下,做备份。

东西比较简单,一个Acitvity,一个layout。下面上代码。


1. FragmentTabHostActivity

package com.test.other;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.test.myapplication.R;

import java.util.List;

public class FragmentTabHostActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_fragment_tab_host);

        final FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        mTabHost.addTab(mTabHost.newTabSpec("0").setIndicator("新闻"), NewsFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("1").setIndicator("音乐"), LifeFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("2").setIndicator("人生"), NewsFragment.class, null);

        ((Button)findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Fragment> fragments=getSupportFragmentManager ().getFragments();
                Fragment fragment=fragments.get(mTabHost.getCurrentTab());
                String str=((TextView)fragment.getView().findViewById(android.R.id.button1)).getText().toString();
                Toast.makeText(FragmentTabHostActivity.this,str,Toast.LENGTH_LONG).show();
            }
        });
    }

    public static class NewsFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            TextView textView = new TextView(getActivity());
            textView.setId(android.R.id.button1);
            textView.setText("NewsFragment");
            return textView;
        }
    }

    public static class LifeFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            TextView textView = new TextView(getActivity());
            textView.setId(android.R.id.button1);
            textView.setText("LifeFragment");
            return textView;
        }
    }

}

2.layout_fragment_tab_host.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.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#abc123"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#555aec"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffaa476d"
                />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click" />
</LinearLayout>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值