您的位置:首页 > 其它

4000 TabHost 和 FragmentTabHost的基本用法

2017-05-19 16:13 447 查看

TabHost

使用布局文件创建TabHost,内容放在一块

<TabHost android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<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"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1"/>

</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2"/>

</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>


java代码

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initTabHost();
}

public void initTabHost() {
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", getDrawable(R.mipmap.ic_launcher)).setContent(R.id.tab1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2", getDrawable(R.mipmap.ic_launcher)).setContent(R.id.tab2));

}

}


注意:

1. TabHost 标签
android:id="@android:id/tabhost"


2. TabWidght 标签
android:id="@android:id/tabs


3. FrameLayout 标签
android:id="@android:id/tabcontent


4. FrameLayout是固定标签所有内容必须写在FrameLayout里面

第二种 布局文件创建,内容分开

1. 布局文件 tab1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1"/>
</LinearLayout>


2. 布局文件 tab2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2"/>
</LinearLayout>


主布局文件

<?xml version="1.0" encoding="utf-8"?>

<TabHost android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<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"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">

</FrameLayout>
</LinearLayout>
</TabHost>


主布局文件 Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initTabHost();
}
public void initTabHost() {
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.tab1,tabHost.getTabContentView());
inflater.inflate(R.layout.tab2,tabHost.getTabContentView());//动态载入XML不需要Activity

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("标签1", getDrawable(R.mipmap.ic_launcher)).setContent(R.id.layout1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("标签2", getDrawable(R.mipmap.ic_launcher)).setContent(R.id.layout2));
}

}


第三种 使用Java代码进行创建TabHost,不推荐。(无代码)

第四种 FragmentTabHost 方式(继承自FragmentActivity)

继承自TabActivity的方式已经过时,推荐使用下面 继承自FragmentActivity**

主文件

<?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.example.mbh.tabhost.FragmentTabsActivity">

<!--存放主要页面内容-->
<FrameLayout
android:id="@+id/maincontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

</FrameLayout>

<!--tab菜单-->
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp">
</FrameLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>


fragment tab1

<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.example.mbh.tabhost.Tab1Fragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment tab1" />

</LinearLayout>


fragment tab2

<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.example.mbh.tabhost.Tab2Fragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment tab2" />

</LinearLayout>


fragment tab对应的Activity

public class Tab1Fragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
}


主布局文件对应Activity

public class FragmentTabsActivity extends FragmentActivity {
private FragmentTabHost tabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment_tabs);

initTabHost();
}

private void initTabHost() {
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.maincontent);
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getDrawable(R.mipmap.ic_launcher)), Tab1Fragment.class, null);
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2", getDrawable(R.mipmap.ic_launcher)), Tab2Fragment.class, null);
}
}


参考1

参考2

参考3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: