关于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是要绑定的内容卡片用的】,大家可以多多试试研究
小弟也是白菜,解释的也不一定对,多多包涵,目前也就只能这么理解了。
代码保准管用,小弟手机测试通过