安卓UI TabActivity问题

3

我正在尝试为应用程序实现以下背景...

enter image description here

对于背景图片(应用程序背景)...我通过将图像添加到setContentView(layout)中来设置图像。但是,由于添加了这一行,我遇到了运行时异常...

如果我在子活动中设置此背景,则无法获得背景以填充整个应用程序背景.. 有什么替代方法吗?

public class HMITabActivity extends TabActivity{
    private TabHost tabHost = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.background);
        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setTabHostColors();
            }
        });
        tabHost.addTab(tabHost.newTabSpec("Tasks")
                .setIndicator("Tasks", getResources().getDrawable(R.drawable.icon_task))
                .setContent(new Intent(this, Tasks.class)));
        tabHost.addTab(tabHost.newTabSpec("HMI")
                .setIndicator("HMI", getResources().getDrawable(R.drawable.icon_hmi))
                .setContent(new Intent(this, HMI.class)));
        tabHost.addTab(tabHost.newTabSpec("Diagnostics")
                .setIndicator("Diagnostics", getResources().getDrawable(R.drawable.icon_diagnostics))
                .setContent(new Intent(this, Diagnostics.class)));
        tabHost.addTab(tabHost.newTabSpec("About")
                .setIndicator("About", getResources().getDrawable(R.drawable.icon_info))
                .setContent(new Intent(this, Tasks.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        Intent intent = new Intent(BackgroundService.class.getName());
        startService(intent); 
    }

    private void setTabHostColors() {
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb(1, 1, 1)); //unselected
        }
        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.rgb(50, 120, 160)); // selected
    }

}

我建议您创建自己的自定义选项卡。这样可以确保在所有设备上看起来都一样。Android选项卡不一定在不同的设备上看起来一样。 - james
哦...是吗?我不知道...谢谢... - Arun Abraham
2个回答

14

要实现这个功能,您需要使用自定义选项卡,在这里是代码,请尝试:

  tabHost= getTabHost();
  tabHost.addTab(tabHost.newTabSpec("tab1").setContent(new Intent(this, Activity2.class)).setIndicator(prepareTabView("Names",R.drawable.icon)));

prepareTabView是一种充气视图的方法。然后可以像这样充气视图:

    private View prepareTabView(String text, int resId) {
         View view = LayoutInflater.from(this).inflate(R.layout.tabs, null);
         ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
         TextView tv = (TextView) view.findViewById(R.id.TabTextView);
         iv.setImageResource(resId);
         tv.setText(text);
         return view;
    }

选项卡的 XML 将如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:id="@+id/TabLayout" 
android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:padding="5dip">

<ImageView android:id="@+id/TabImageView" android:src="@drawable/icon"           
 android:layout_width="wrap_content" android:layout_height="wrap_content"/>

<TextView android:id="@+id/TabTextView" android:text="Text" 
android:paddingTop="5dip" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="@color/black" 
android:textAppearance="@style/TabTextViewStyle" />

 </LinearLayout>

然后现在按照您喜欢的方式添加背景颜色。


不错...但如果我想改变现成选项卡的文本颜色,那该怎么办? - user644458
@Venky:不错。我这边点赞。但是现在我想要在选中或点击时更改 tabBar 的颜色,怎么办? - Shreyash Mahajan

0

TabActivity在Android 4.2,API级别17中已被弃用。请使用Fragments代替TabActivity。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接