首先,我对 android 非常陌生,我对此了解不多。我正在尝试使用它,我现在正在关注本教程: http ://www.androidhive.info/2011/08/android-标签布局教程/
我创建了一个类如下
package com.example.tabbedactivity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabbedActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
// setting Title and Icon for the Tab
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos ta
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_tabbed, menu);
return true;
}
}
因此我在drawable下制作了三个xml文件,如下所示icon_photos_tab.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/photo-hover"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/photo-unhover" />
</selector>
icon_songs_tab.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/music-hover"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/music-unhover" />
</selector>
icon_videos_tab.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/video-hover"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/video-unhover" />
</selector>
现在,当我尝试在 drawable 下访问这些 xml 文件时 IDE 出现问题,这正是下面显示的代码行,它们是 TabbedActivity.java 的一部分(我在上面显示的类)
TabSpec songspec = tabHost.newTabSpec("Songs");
songspec.setIndicator("Songs", getResources().getDrawable(R.drawable.icon_songs_tab));
TabSpec photospec = tabHost.newTabSpec("Photos");
// setting Title and Icon for the Tab
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
TabSpec videospec = tabHost.newTabSpec("Videos");
videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
错误说
此行有多个标记 - icon_photos_tab 无法解析或不是字段 - R.drawable 无法解析为变量
究竟可能是什么问题。(我对android真的很陌生。这有点像我的第一个应用程序,我还没有经过任何理论)
更新我在控制台上收到以下错误
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-ldpi\video-hover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-mdpi\video-hover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-xhdpi\video-hover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-hdpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-ldpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-mdpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]
[2013-01-12 12:32:02 - TabbedActivity] res\drawable-xhdpi\video-unhover.png: Invalid file name: must contain only [a-z0-9_.]