Android选项卡图像不适用于选项卡

Android选项卡图像不适用于选项卡,android,tabs,Android,Tabs,我正在开发带有标签的android应用程序。我已经完成了实现,但我面临一个问题。图像无法放在标签上 这是我的java代码: import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; impo

我正在开发带有标签的android应用程序。我已经完成了实现,但我面临一个问题。图像无法放在标签上

这是我的java代码:

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.tv.allies.FriendsScreen;
import com.tv.servercommunication.IServerResponse;
import com.tv.setting.SettingActivity;
import com.tv.socialgoal.R;
import com.tv.socialgoal.profile.ProfileScreen;
import com.tv.task.TaskListActivity;

@SuppressWarnings("deprecation")
public class TabViewActivity extends TabActivity implements IServerResponse{
    private static final String INBOX_SPEC = "Inbox";

    private static final String OUTBOX_SPEC = "Outbox";
    private static final String PROFILE_SPEC = "Profile";
    private static final String SETTING_SPEC = "Setting";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost
                .newTabSpec("one")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.missions_btn_up))
                        .setContent(
                                new Intent(this, TaskListActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC);

        tabHost.addTab(tabHost
                .newTabSpec("two")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.allies_btn_up))
                        .setContent(new Intent(this, FriendsScreen.class)));

        TabSpec setting = tabHost.newTabSpec(OUTBOX_SPEC);
        outboxSpec.setIndicator("",getResources().getDrawable(R.drawable.profile_btn_up));
        Intent outboxIntent = new Intent(this, ProfileScreen.class);
        outboxSpec.setContent(outboxIntent);

        // Profile Tab
        TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC);
        profileSpec.setIndicator("",getResources().getDrawable(R.drawable.settings_btn_up));
        Intent profileIntent = new Intent(this, SettingActivity.class);
        profileSpec.setContent(profileIntent);

        tabHost.addTab(outboxSpec); // Adding Outbox tab
        tabHost.addTab(profileSpec);
    }

    @Override
    public void serverResponse(String response, int processid) {
        // TODO Auto-generated method stub

    }

}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:background="#3399FF"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</TabHost>
这里是标签xml:

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.tv.allies.FriendsScreen;
import com.tv.servercommunication.IServerResponse;
import com.tv.setting.SettingActivity;
import com.tv.socialgoal.R;
import com.tv.socialgoal.profile.ProfileScreen;
import com.tv.task.TaskListActivity;

@SuppressWarnings("deprecation")
public class TabViewActivity extends TabActivity implements IServerResponse{
    private static final String INBOX_SPEC = "Inbox";

    private static final String OUTBOX_SPEC = "Outbox";
    private static final String PROFILE_SPEC = "Profile";
    private static final String SETTING_SPEC = "Setting";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost
                .newTabSpec("one")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.missions_btn_up))
                        .setContent(
                                new Intent(this, TaskListActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC);

        tabHost.addTab(tabHost
                .newTabSpec("two")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.allies_btn_up))
                        .setContent(new Intent(this, FriendsScreen.class)));

        TabSpec setting = tabHost.newTabSpec(OUTBOX_SPEC);
        outboxSpec.setIndicator("",getResources().getDrawable(R.drawable.profile_btn_up));
        Intent outboxIntent = new Intent(this, ProfileScreen.class);
        outboxSpec.setContent(outboxIntent);

        // Profile Tab
        TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC);
        profileSpec.setIndicator("",getResources().getDrawable(R.drawable.settings_btn_up));
        Intent profileIntent = new Intent(this, SettingActivity.class);
        profileSpec.setContent(profileIntent);

        tabHost.addTab(outboxSpec); // Adding Outbox tab
        tabHost.addTab(profileSpec);
    }

    @Override
    public void serverResponse(String response, int processid) {
        // TODO Auto-generated method stub

    }

}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:background="#3399FF"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</TabHost>

这里是屏幕简短:

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.tv.allies.FriendsScreen;
import com.tv.servercommunication.IServerResponse;
import com.tv.setting.SettingActivity;
import com.tv.socialgoal.R;
import com.tv.socialgoal.profile.ProfileScreen;
import com.tv.task.TaskListActivity;

@SuppressWarnings("deprecation")
public class TabViewActivity extends TabActivity implements IServerResponse{
    private static final String INBOX_SPEC = "Inbox";

    private static final String OUTBOX_SPEC = "Outbox";
    private static final String PROFILE_SPEC = "Profile";
    private static final String SETTING_SPEC = "Setting";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost
                .newTabSpec("one")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.missions_btn_up))
                        .setContent(
                                new Intent(this, TaskListActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC);

        tabHost.addTab(tabHost
                .newTabSpec("two")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.allies_btn_up))
                        .setContent(new Intent(this, FriendsScreen.class)));

        TabSpec setting = tabHost.newTabSpec(OUTBOX_SPEC);
        outboxSpec.setIndicator("",getResources().getDrawable(R.drawable.profile_btn_up));
        Intent outboxIntent = new Intent(this, ProfileScreen.class);
        outboxSpec.setContent(outboxIntent);

        // Profile Tab
        TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC);
        profileSpec.setIndicator("",getResources().getDrawable(R.drawable.settings_btn_up));
        Intent profileIntent = new Intent(this, SettingActivity.class);
        profileSpec.setContent(profileIntent);

        tabHost.addTab(outboxSpec); // Adding Outbox tab
        tabHost.addTab(profileSpec);
    }

    @Override
    public void serverResponse(String response, int processid) {
        // TODO Auto-generated method stub

    }

}
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:background="#3399FF"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</TabHost>


如何将所有选项卡图像放入每个选项卡?

根据分辨率在选项卡中使用适当的图像


使用以下链接生成选项卡图像。

回答您的问题是一件棘手的事情。要根据您的要求设置选项卡背景和图像,您必须使用必须在选项卡上设置的自定义图像xml

1) 用于选项卡背景

a) 在drawable文件夹中创建一个xml并复制粘贴以下内容

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item android:state_selected="true" android:drawable="@drawable/tab_bg_green" />
    <item android:drawable="@drawable/tab_bg_blue" />
</selector>
如果这对您有帮助,请告诉我。

@ricintech检查一下