FragmentTabHost的使用(上面切换)
效果:这是显示在上面
布局:
xmlns:
tools
=
"http://schemas.android.com/tools"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
tools :context= ".activity.MainActivity" >
<android.support.v4.app.FragmentTabHost
android :id= "@+id/tab"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
>
<!-- 以下的布局是固定写法
android:id="@android:id/tabs 表示指向安卓的某个 ID">-->
<LinearLayout
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :orientation= "vertical"
>
<!-- 用于切换的 view-->
<TabWidget
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :id= "@android:id/tabs" >
</TabWidget>
<!-- 每切换一次显示的 Fragment-->
<FrameLayout
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :id= "@android:id/tabcontent"
></FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
android :layout_width= "match_parent"
android :layout_height= "match_parent"
tools :context= ".activity.MainActivity" >
<android.support.v4.app.FragmentTabHost
android :id= "@+id/tab"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
>
<!-- 以下的布局是固定写法
android:id="@android:id/tabs 表示指向安卓的某个 ID">-->
<LinearLayout
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :orientation= "vertical"
>
<!-- 用于切换的 view-->
<TabWidget
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :id= "@android:id/tabs" >
</TabWidget>
<!-- 每切换一次显示的 Fragment-->
<FrameLayout
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :id= "@android:id/tabcontent"
></FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
FragmentClock布局:
<?
xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :orientation= "horizontal"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :gravity= "center" >
<TextView
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "clock"
android :id= "@+id/clock"
android :textColor= "@android:color/black"
android :textSize= "22sp"
/>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android :orientation= "horizontal"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :gravity= "center" >
<TextView
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :text= "clock"
android :id= "@+id/clock"
android :textColor= "@android:color/black"
android :textSize= "22sp"
/>
</LinearLayout>
Mainactivity代码
public class
MainActivity
extends
AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main)
FragmentTabHost tab = (FragmentTabHost) findViewById(R.id.
tab
)
;
// 第三个参数 android.R.id.tabcontent 是要放的内容的控件 ID ,也就是 FragmentLayout
// 这个是初始化,必须要先初始化
tab.setup( this, getSupportFragmentManager() , android.R.id. tabcontent ) ;
// 第三个参数 android.R.id.tabcontent 是要放的内容的控件 ID ,也就是 FragmentLayout
// 这个是初始化,必须要先初始化
tab.setup( this, getSupportFragmentManager() , android.R.id. tabcontent ) ;
// 第一个参数设置
每一个选项卡
外观,第二个是设置要跳到哪一个页面
tab.addTab(tab.newTabSpec(
"1"
).setIndicator(
"one"
)
,
FragmentClock.
class,null
)
;
tab.addTab(tab.newTabSpec( "2" ).setIndicator( "two" ) , FragmentClock. class,null ) ;
tab.addTab(tab.newTabSpec( "3" ).setIndicator( "three" ) , FragmentClock. class,null ) ;
tab.addTab(tab.newTabSpec( "4" ).setIndicator( "four" ) , FragmentClock. class,null ) ;
tab.addTab(tab.newTabSpec( "5" ).setIndicator( "five" ) , FragmentClock. class,null ) ;
tab.addTab(tab.newTabSpec( "2" ).setIndicator( "two" ) , FragmentClock. class,null ) ;
tab.addTab(tab.newTabSpec( "3" ).setIndicator( "three" ) , FragmentClock. class,null ) ;
tab.addTab(tab.newTabSpec( "4" ).setIndicator( "four" ) , FragmentClock. class,null ) ;
tab.addTab(tab.newTabSpec( "5" ).setIndicator( "five" ) , FragmentClock. class,null ) ;
}
FragmentClock
代码
public class
FragmentClock
extends
Fragment {
@Nullable
@Override
public View onCreateView (LayoutInflater inflater , @Nullable ViewGroup container , @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout. fragment_clock ,null ) ;
}
@Nullable
@Override
public View onCreateView (LayoutInflater inflater , @Nullable ViewGroup container , @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout. fragment_clock ,null ) ;
}
}