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
- android FragmentTabHost 的简单使用
- android FragmentTabHost的简单使用
- android FragmentTabHost 的简单使用
- Android-FragmentTabHost的简单使用
- FragmentTabHost的简单使用
- FragmentTabHost的简单使用
- FragmentTabHost的简单使用
- android FragmentTabHost的使用
- android fragmentTabHost的使用
- 【FragmentTabHost】FragmentTabHost的使用
- Android 布局FragmentTabHost的使用
- Android之---FragmentTabHost使用简单介绍
- Android仿新浪微博底栏(FragmentTabHost的使用)
- Android常用控件之FragmentTabHost的使用
- Android仿新浪微博底栏(FragmentTabHost的使用)
- Android常用控件之FragmentTabHost的使用
- android FragmentTabHost的使用技巧及注意事项
- FragmentTabHost的简单应用
- LeetCode N-Queens
- Android bluetooth介绍(一):基本概念及硬件接口
- 简单二叉树
- 工作十年的老程序员忠告
- memcached全面剖析–3. memcached的删除机制和发展方向
- android FragmentTabHost的简单使用
- nyoj 623
- 如果无法用gitk显示git的版本树,可通过git命令显示版本树图
- mac锁屏快捷键
- 今天开始重新拾起写Blog的习惯
- 向量的叉积和点积的 几何意义 有关于投影的推导(和点积的关系)
- 母函数&&排列(模板)
- Ukkonen后缀树构造算法——第一部分
- Jquery選擇器