android - 我如何在中心 tabhost 设置图像?

标签 android android-tabhost

有人知道我如何在中心选项卡中设置图像吗? 我试图更改我的 xml 但没有成功。

在我的 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">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>
    </RelativeLayout>
</TabHost>

在我的代码 Activity 下方:

@SuppressWarnings("deprecation")
        final
        TabHost tabHost = getTabHost();

        // Tab 1
        TabSpec aba1spec = tabHost.newTabSpec("Tab 1");
        // setting Title and Icon for the Tab
        tabHost.getTabWidget().setStripEnabled(false);
        aba1spec.setIndicator("",getResources().getDrawable(R.drawable.tab1));
        Intent tab1 = new Intent(this, tab1.class);
        aba1spec.setContent(tab1);

        // Adding all TabSpec to TabHost
        tabHost.addTab(aba1spec); // Adding tab1

图片:

enter image description here

非常感谢。

最佳答案

1)- 创建一个名为 my_xml.xml 或您喜欢的任何名称的布局文件。

在 report_tabs 中使用此代码。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/layout_tabsLayout"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/btn_selector"
 android:gravity="center"
 android:orientation="vertical"
 >

 <ImageView
 android:id="@+id/img_icon"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginTop="3dp"
 />
</LinearLayout>

2)- 并在您的 Activity 中使用以下代码。

Intent intent;
tabhost = getTabHost();
TabHost.TabSpec tabspec;
intent = new Intent().setClass(getApplicationContext(),xxxxx.class);

tabspec = tabhost.newTabSpec("First");
view = LayoutInflater.from(this).inflate(R.layout.report_tabs,
tabhost.getTabWidget(), false);
imgtabF = (ImageView) view.findViewById(R.id.img_icon);
imgtabF.setBackgroundResource(R.drawable.tab_icon_selector);

tabspec.setIndicator(view);
tabspec.setContent(intent);
tabhost.addTab(tabspec);

3)- 在 drawable 中创建一个名为 tab_icon_selector 的文件,用于更改选项卡上的图标,如下所示:-

<?xml version="1.0" encoding="utf-8"?>
<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/medical_icon_unselect"    
 />
 <item android:state_focused="false" android:state_selected="true" 
 android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
 <!-- Focused states -->
 <item android:state_focused="true" android:state_selected="false" 
 android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" />
 <item android:state_focused="true" android:state_selected="true" 
 android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
 <!-- Pressed -->
 <item android:state_pressed="true" android:drawable="@android:color/transparent"/> 
 </selector>

现在您可以创建自定义标签栏,您的图像图标将位于标签的中央。

source :

关于android - 我如何在中心 tabhost 设置图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26554587/

相关文章:

android - 是否可以在后台定位服务已经启动后更改刷新率?

android - 使用谷歌照片的裁剪 Intent 不起作用

android - 单选按钮获取选项卡的功能

android tabHost 在每个选项卡内有许多 Activity

android - 我可以在自定义选项卡 View 中使用默认选项卡样式吗?

android - ORMLite 和图像在 Android 上保存为 BLOB

java - 我对 sqlite ntable 进行了更改,我想知道如何在 onupgrade 中更新它?

android - 安卓模拟追踪

Android Tabhost 内容在平板电脑上卡住但在模拟器中不卡住

java - 现在不推荐使用 TabActivity 如何实现选项卡