android FragmentTabHost的简单使用

来源:互联网 发布:遇事不要慌,知乎 编辑:程序博客网 时间:2024/06/06 10:47

在一篇博客里看到一个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
原创粉丝点击