2

好的,所以我正在制作一个带有标签的 android 应用程序,现在我的问题是标签小部件在不同的 android 版本或设备中并不统一。我想让它在任何 android 上都一样这是我的标签活动

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Cook extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cook_layout);

  TabHost tabHost = getTabHost();

    // Tab for Snacks
    TabSpec snackspec = tabHost.newTabSpec("Snacks");
    // setting Title and Icon for the Tab
    snackspec.setIndicator("Snacks", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
    Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
    snackspec.setContent(snacksIntent);



    // Tab for Mains
    TabSpec mainspec = tabHost.newTabSpec("Mains");
    mainspec.setIndicator("Mains", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
    Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
    mainspec.setContent(mainsIntent);

    // Tab for Desserts
    TabSpec dessertspec = tabHost.newTabSpec("Desserts");
    dessertspec.setIndicator("Desserts", getResources().getDrawable(R.drawable.cook_icon_tab_snacks));
    Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
    dessertspec.setContent(dessertsIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(snackspec); // Adding snacks tab
    tabHost.addTab(mainspec); // Adding mains tab
    tabHost.addTab(dessertspec); // Adding desserts tab
}

}

我也有我的 XML 布局:

<?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:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:background="@drawable/gradient_bg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

我制作了一个新的指标 xml,它就像我关注的主要 android 选项卡指示器 v4 并搜索了很多博客,但我找不到我的答案......我真的想让所有 android 版本的 android 选项卡统一并制作颜色不错,因为橙色和黄色不适合我的应用程序中的颜色主题请帮助!!!!我似乎无法找到解决方法...干杯

4

1 回答 1

3

好的,我找到了解决方案。这是代码:

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Cook extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cook_layout);

  TabHost tabHost = getTabHost();

    // Tab for Snacks
    TabSpec snackspec = tabHost.newTabSpec("Snacks");
    // setting Title and Icon for the Tab
    snackspec.setIndicator(makeTabIndicator(getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
    Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
    snackspec.setContent(snacksIntent);



    // Tab for Mains
    TabSpec mainspec = tabHost.newTabSpec("Mains");
    mainspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
    Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
    mainspec.setContent(mainsIntent);

    // Tab for Desserts
    TabSpec dessertspec = tabHost.newTabSpec("Desserts");
    dessertspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
    Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
    dessertspec.setContent(dessertsIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(snackspec); // Adding snacks tab
    tabHost.addTab(mainspec); // Adding mains tab
    tabHost.addTab(dessertspec); // Adding desserts tab
}


//making the tab view:
private View makeTabIndicator(Drawable drawable){
ImageView Tabimage = new ImageView(this);
LayoutParams LP = new           LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
LP.setMargins(1, 0, 1, 0);
Tabimage.setLayoutParams(LP);
Tabimage.setImageDrawable(drawable);
Tabimage.setBackgroundResource(R.drawable.tabview);
return Tabimage;


}}

我不知道天气与否我不再需要cook_layout 我会看看我是否可以将它移除或稍后离开......现在我只想让它全部正常工作,稍后我会过来打扫卫生希望对那些偶然发现这个问题的人有所帮助!干杯

于 2012-03-15T18:48:36.330 回答