【问题标题】:Custom TabWidget Android Tab Indicator自定义 TabWidget Android 标签指示器
【发布时间】:2012-03-06 21:17:07
【问题描述】:

好的,所以我正在制作一个具有标签的 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 选项卡统一并使颜色漂亮,因为橙色和黄色并不真正适合我的应用程序中的颜色主题 请帮忙!!!! 我似乎找不到修复它的方法... 干杯

【问题讨论】:

    标签: android tabs customization indicator android-tabs


    【解决方案1】:

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

    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 我会看看我是否可以删除它或稍后离开它......现在我只想让它全部工作,稍后我会过来看看打扫卫生 希望对那些偶然发现这个问题的人有所帮助!干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多