【问题标题】:Android tabview icon with background color带有背景颜色的 Android tabview 图标
【发布时间】:2012-02-14 05:17:13
【问题描述】:

我正在尝试在 android 中创建一个带有图标和背景的 tabview。但不能设置标签的背景。

java

intent = new Intent().setClass(this, mainActivity.class);   
spec = tabHost.newTabSpec("main").setIndicator("Main", res.getDrawable(R.drawable.ic_tab_main)).setContent(intent);   
tabHost.addTab(spec); 

xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:drawable="@drawable/cameraiconblack"        
          android:state_selected="true" /> 
    <item android:drawable="@drawable/cameraiconwhite" />
</selector>

【问题讨论】:

    标签: android background tabview


    【解决方案1】:

    提供这个

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false"     android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" />
    

    然后在tab_unselected.xml中写入

     <shape xmlns:android="http://schemas.android.com/apk/res/android" >
     <solid android:color="#ccc"/>
    
    
     </shape>
    

    & for tab_selected.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#ccc"/>
    
    
    </shape>
    

    现在为图像和文本视图或任何你想要的背景制作布局文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="64dip"    
    android:layout_weight="1"
    android:orientation="vertical"
     android:background="@drawable/tab_indicator"
    android:padding="5dp">
    
    <ImageView android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    
    />
    
    <TextView android:id="@+id/title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
        android:gravity="center"
        style="?android:attr/tabWidgetStyle"
    />    
    

    现在像这样更改您的主标签文件

    将 setTabs() 方法放入你的 oncreate 中。

    private void setTabs() {
        // TODO Auto-generated method stub
        tabHost = getTabHost();
        tabHost.getTabWidget().setDividerDrawable(R.drawable.line);
        intent = new Intent(this, class1.class);
        addTab(1,intent,"tab1");
        intent=new Intent(this,class2.class);
        addTab(2,intent,"tab2");
        intent=new Intent(this,class3.class);
        addTab(3,intent,"tab3");
    
    }
    

    现在 addTab 方法

    private void addTab(int labelId, Intent intent, String tabName) {
        // TODO Auto-generated method stub
     TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);        
    
       View tabIndicator =LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    
        TextView title = (TextView) tabIndicator.findViewById(R.id.title1);
        title.setTextSize(40);
        title.setText(tabName);
        ImageView imageView=(ImageView)tabIndicator.findViewById(R.id.icon);
        imageView.setImageResource(R.drawable.aries);
    
    
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    
    }
    

    我想我已经输入了所有需要的代码。现在享受

    【讨论】:

    • 但是如何添加图标,我应该在 tab_selected.xml 和 tab_unselected.xml 下添加 android:drawable="@drawable/tab_icon"
    • 您必须为图像指定布局。然后在您的类中调用此图像,如 ImageView imageView=(ImageView)tabIndicator.findViewById(R.id.icon); imageView.setImageResource(R.drawable.aries);它将在背景中设置图像
    【解决方案2】:
    TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent; 
    

    查看 view_temples= LayoutInflater.from(getBaseContext()).inflate(R.layout.tab_view, null);

    spec = tabHost.newTabSpec("main").setIndicator(view_temples.findViewById(R.id.your_image_layout_temple)) .setContent(意图); tabHost.addTab(spec);

    【讨论】:

    • ---tab_view.xml---- schemas.android.com/apk/res/android" android:id= "@+id/your_image_layout_temple" android:layout_width="fill_parent" android:layout_height="fill_parent" >
    • 和 -----your_selector.xml------ schemas.android.com/apk/res/android ">
    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多