2011-04-06 41 views
3

我想实现应用程序的以下背景... enter image description hereAndroid的UI TabActivity问题

为背景图像(应用背景)...我设定的setContentView(布局)图像..通过添加这一行,我得到一个运行时异常...

如果我在子活动中设置此背景..我不会得到背景来填充完整的应用程序背景..任何想法什么是替代?

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 
} 

}

+0

我建议创建自己的自定义选项卡。这样它将保证在所有设备上看起来相同。 Android标签并不总是在不同的设备上看起来相同 – binnyb 2011-04-06 14:03:51

+0

哦......是吗?我没有意识到它...谢谢... – 2011-04-06 14:09:21

回答

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> 

,只要你喜欢。然后现在添加您的backgroung颜色..

+0

谢谢....这是更灵活.. – 2011-04-06 14:38:09

+0

很好听到... – Venky 2011-04-06 14:40:52

+0

不错的一个...但如果我想改变颜色的准备好的制表符文本颜色然后? – 2011-04-21 09:53:17

0

TabActivity从Android电子弃用4.2,API等级17.使用Fragments而不是TabActivity