Android Tutorials-----Tab Layout ,错误提示 tabHost.setCurrentTab(getIntent());
2010-03-14 23:22
671 查看
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/20/5e6b61cc72b2e22d772f84d6257510b6.png)
今天晚上我按照android开发网,做一个Tab Layout的实例,却总是无法成功!
没道理官方网站的教程都是错的啊!!
结果经过我的不段的测试,总算成功了!也把心里体会拿出来与大家分享!
原教程:http://developer.android.com/intl/zh-CN/resources/tutorials/views/hello-tabwidget.html
运行环境:
eclipse 3.4
android 1.6
如果按照上面所做,不会成功的!其中还缺少重要文件和代码,我将重要步骤贴出:
1.Start a new project named HelloTabWidget
. (不变)
2.分别建立三个java:
ArtistsActivity
,
AlbumsActivity
, and
SongsActivity
public class ArtistsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Artists tab"); setContentView(textview); } }
我们只需将其中的"This is the Artists tab" 中的Artists替换成Albums,Songs
3.在res/drawable/(有的是drawable-mdpi),下面新建:三个图片(png格式,且是透明的)分别为ic_tab_artists.png,ic_tab_albums.png,ic_tab_songs.png。如果没有手边没有png格式图片的话,可以到(android安装目录/platforms/android-1.6/data/res/drawable/)下面找,很多!
重要的一点,其实并不需要想教程所说建立
ic_tab_artists.xml,完全没有必要的!我自认为!
请让我给你分析:建立Tab Layout时,选中的图标背景会成白色。如果没有选中的图标,背景会成灰色。而我们找的icon图标本身是浅灰色。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/20/ae329ec2cf2d411545b15190d3a404c7.jpg)
4.Open the
res/layout/main.xml
file and insert the following:(不变)
5Now open
HelloTabWidget.java
and make it extend
TabActivity
:.
package com.hello.tabview; import android.app.TabActivity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.widget.TabHost; public class HelloTabWidget extends TabActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Reusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, ArtistsActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("A").setIndicator("文章", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); //TabSpec android.widget.TabHost.TabSpec.setContent(Intent intent) //newTabSpec("mytag")就是为后面的setCurrentTabByTag("mytag") //setIndicator(label,icon);label就是上面显示的文字 tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this, AlbumsActivity.class); spec = tabHost.newTabSpec("B").setIndicator("相片", res.getDrawable(R.drawable.ic_tab_albums)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("C").setIndicator("歌曲", res.getDrawable(R.drawable.ic_tab_songs)) .setContent(intent); tabHost.addTab(spec); //tabHost.setCurrentTab(0);//默认tab是从0开始,设置当前的tab标签 tabHost.setCurrentTabByTag("B"); } }
6.还有一个关键的地方:就是修改AndroidManifest.xml文件,挨着<activity android:name=".
HelloTabWidget
" .....>....</acitivity>后面添加代码:
<activity android:name=".AlbumsActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> </activity> <activity android:name=".ArtistsActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> </activity> <activity android:name=".SongsActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> </activity>
7.然后运行项目,得到如下图片。(可能你们的有点区别)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/20/699c78d51f123b0483df8c3981e5b7f1.png)
呵呵,总算成功了。
不过我还发现一个问题,但是没有解决。
问题:如果你按下主页图标
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/20/aa9b6217a0c67f16f34333dfc15ae9c0.png)
,然后再进入此应用,tab标签仍留在推出的状态
但是如果你按下后退键
![](https://oscdn.geek-share.com/Uploads/Images/Content/201909/20/c7b7f97026279af18d9f8f8372642acf.png)
,再进入,tab标签还原成默认状态。
现在没找到答案,有哪位兄弟能够给我解答!!
相关文章推荐
- Android TabHost设置setCurrentTab(index),当index!=0时,默认加载第一个tab问题解决方法。
- Android Tab Layout (TabHost) Tutorial的Force Close错误
- eclipse 错误提示API level 11 (current min is 8)android.app.Activity#getActionBar解决办法
- eclipse 刚建的android项目就提示错误:setContentView(R.layout.main);R下边是红线。怎么解决呢?
- Android顶部导航之TabLayout和ViewPager的组合,setupWithViewPager有点坑
- Android Studio中提示:Call requires API level 16 (current min is 15): android.view.View#setBackground
- Android TabLayout setCustomView 实现带图标的tab
- GTK+乱码错误,提示:Invalid UTF-8 string passed to pango_layout_set_text()
- android layout 提示XML解析错误
- Android开发值利用Intent进行put传值,setclass启动activity,并用get进行取值
- Android特效专辑(四)——APP主页框架TabHost绑定ViewPager的替换者TabLayout
- Android TabLayout setCustomView 实现带图标的tab
- android自定义tabhost,tabcontent用intent获得
- Android TabLayout setCustomView 实现带图标的tab
- android自定义tabhost,tabcontent用intent获得
- TabLayout中的getSelectedTabPosition()方法返回值错误
- Get “android:layout_height” from `AttributeSet` in a custom view constructor
- android_TabLayout设置setupWithViewPager需要注意的问题
- Android使用TabHost程序异常终止could not create tab content because could not find view with id 错误
- Android中使用setBackgroundDrawable错误提示:The method setBackgroundDrawable(Drawable) from the type View is