Android笔记:通过RadioGroup/RadioButton自定义tabhost的简单方法

简介:

XML代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<RelativeLayout 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"
     tools:context= ".MainActivity"  >
     <TabHost
         android:id= "@android:id/tabhost"
         android:layout_width= "match_parent"
         android:layout_height= "match_parent"
         android:layout_alignParentLeft= "true"
         android:layout_alignParentTop= "true"  >
         <LinearLayout
             android:layout_width= "match_parent"
             android:layout_height= "match_parent"
             android:orientation= "vertical"  >
             <TabWidget
                 android:id= "@android:id/tabs"
                 android:layout_width= "match_parent"
                 android:layout_height= "wrap_content"
                 android:visibility= "gone"  >
             </TabWidget>
             <FrameLayout
                 android:id= "@android:id/tabcontent"
                 android:layout_width= "match_parent"
                 android:layout_height= "0dp"
                 android:layout_weight= "1"  >
             </FrameLayout>
             <RadioGroup
                 android:layout_width= "match_parent"
                 android:layout_height= "wrap_content"
                 android:layout_gravity= "bottom"
                 android:orientation= "horizontal"  >
                 <RadioButton
                     android:id= "@+id/maintab_radio_tab1"
                     style= "@style/tab_button"
                     android:checked= "true"
                     android:text= "主页"
                     android:background= "#ff00ff00"  />
                 <RadioButton
                     android:id= "@+id/maintab_radio_tab2"
                     style= "@style/tab_button"
                     android:text= "分页"
                     android:background= "#ff00ff00"  />
                 <RadioButton
                     android:id= "@+id/maintab_radio_tab3"
                     style= "@style/tab_button"
                     android:text= "设置"
                     android:background= "#ff00ff00"  />
             </RadioGroup>
         </LinearLayout>
     </TabHost>
</RelativeLayout>


style="@style/tab_button"文件:

1
2
3
4
5
6
7
8
9
10
11
12
     <!-- 主页底部RadioButton样式 -->
<style name= "tab_button" >
     <item name= "android:layout_width" >fill_parent</item>
     <item name= "android:layout_height" >wrap_content</item>
     <item name= "android:ellipsize" >marquee</item>
     <item name= "android:button" > @null </item>
     <item name= "android:paddingTop" >1dp</item>
     <item name= "android:layout_weight" > 1.0 </item>
     <item name= "android:gravity" >center_horizontal</item>
     <item name= "android:singleLine" > true </item>
     <item name= "android:drawablePadding" > 1 .0dip</item>
</style>


布局结构:(将TabWidget隐藏,用一组去掉button勾选框的RadioButton替代


000054774.png



java代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public  class  MainActivity  extends  TabActivity  implements  OnClickListener
{
       
     private  RadioButton mBtn_tab1;
     private  RadioButton mBtn_tab2;
     private  RadioButton mBtn_tab3;
     private  TabHost mTabHost;
     @Override
     protected  void  onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
           
         init();
         initTab();
     }
     //初始化界面
     private  void  init()
     {
         mBtn_tab1 = (RadioButton)findViewById(R.id.maintab_radio_tab1);
         mBtn_tab2 = (RadioButton)findViewById(R.id.maintab_radio_tab2);
         mBtn_tab3 = (RadioButton)findViewById(R.id.maintab_radio_tab3);
           
           
         mBtn_tab1.setOnClickListener( this );
         mBtn_tab2.setOnClickListener( this );
         mBtn_tab3.setOnClickListener( this );
     }
     //初始化底部标签栏
     private  void  initTab()
     {
         mTabHost = getTabHost();
         mTabHost.addTab(mTabHost.newTabSpec( "tab1" ).setIndicator( "tab1" ).setContent( new  Intent( this , Tab1Activity. class )));
         mTabHost.addTab(mTabHost.newTabSpec( "tab2" ).setIndicator( "tab2" ).setContent( new  Intent( this , Tab2Activity. class )));
         mTabHost.addTab(mTabHost.newTabSpec( "tab3" ).setIndicator( "tab3" ).setContent( new  Intent( this , Tab1Activity. class )));
     }
       
       
     @Override
     public  void  onClick(View v)
     {
         switch  (v.getId())
         {
             case  R.id.maintab_radio_tab1:
                 mTabHost.setCurrentTabByTag( "tab1" );
                 break ;
                   
             case  R.id.maintab_radio_tab2:
                 mTabHost.setCurrentTabByTag( "tab2" );
                 break ;
                   
             case  R.id.maintab_radio_tab3:
                 mTabHost.setCurrentTabByTag( "tab3" );
                 break ;
               
             default :
                 break ;
         }
     }
}





本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1281759,如需转载请自行联系原作者

目录
相关文章
|
4天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
23天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
3月前
|
Android开发 Python
Python封装ADB获取Android设备wifi地址的方法
Python封装ADB获取Android设备wifi地址的方法
61 0
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
2天前
|
存储 Java API
Android系统 文件访问权限笔记
Android系统 文件访问权限笔记
29 1
|
2天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
19 1
|
2天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
19 0
|
2天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
16 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
23天前
|
Android开发
Android调用相机与相册的方法2
Android调用相机与相册的方法
17 0