【androidstudio2.3】tabhost基本使用方法(2017年3月:测试成功)

关于androidstudio2.3版本,tabhost使用教程网上好多都过时用不了,作为菜鸟今天研究一天,总算弄明白了,所以写出来分享O(∩_∩)O~

测试成功:2017年3月

 

唯一需要修改的地方如下图片------------------------------

 

唯一需要修改的地方如下图片------------------------------

-------------------------------------------------------------

 

                                        首先贴出【activity_main.xml】代码 如下                                         

<?xml version="1.0" encoding="utf-8"?>
<TabHost 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:id="@android:id/tabhost"
    tools:context="com.hi347.app.chuanpu.MainActivity">

            <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" />

                <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:background="#ff0000"
                        android:orientation="vertical">

                    </LinearLayout>

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

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/tab3"
                        android:background="#000000"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

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

 

                                            然后贴出【MainActivity.java】代码 如下                               

package com.hi347.app.chuanpu;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabHost tabhost = (TabHost)findViewById(android.R.id.tabhost);
        tabhost.setup();

        tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("红色").setContent(R.id.tab1));

        tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("粉色").setContent(R.id.tab2));

        tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("黑色").setContent(R.id.tab3));


    }
}

 

懒得听得直接复制粘贴上面代码使用

想进一步了解的可以接着看下去

--------------------------------------------------------------------

xml代码中的问题点

在xml代码中,我们要用到的是如下图片组件

然后切换到Text模式,进行代码修改

 

温馨提示:

TabHost的id 必须为     android:id="@android:id/tabhost"
TabWidget的id必须为     android:id="@android:id/tabs"
FrameLayout的id必须为   android:id="@android:id/tabcontent"

如果id不是如上,手动修改吧

 

---------------------------------------------------------------

java代码中的问题点

圈1:第一行代码,网友说要用【TabActivity】,可是今天谷歌不推荐使用【TabActivity】,然后有人就推荐使用【FragmentActivity】,用了之后就是闪退,最终使用的是【Activity】,不需要在前面添加,然后需要添加蓝色圈圈中的代码【tabhost.setup();】

 

圈2:网上的代码如下【TabHsot tabhost = getTabHost()】,其中getTabHost()是红色字体,就算按住ALT+Enter也没用,所以采用【(TabHost)findViewById(android.R.id.tabhost)】替代

 

圈3:这行代码的意思是:【绿色的“tab1”是内容区的名称,用来区别用的】,【绿色的“中文字”是能够实际看到的文字,也就是标签字】【紫色的tab1是要绑定的内容卡片用的】,大家可以多多试试研究

 

小弟也是白菜,解释的也不一定对,多多包涵,目前也就只能这么理解了。

代码保准管用,小弟手机测试通过

 

 

转载于:https://my.oschina.net/u/3337216/blog/854933

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值