其他分享
首页 > 其他分享> > Android UI TabActivity问题

Android 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
    }

}

解决方法:

为此,您必须使用自定义选项卡,这是代码尝试这样:

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

其中prepareTabView是Inflate View的方法.
然后膨胀这样的视图:

    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>

然后现在添加你喜欢的背景颜色..

标签:android,android-layout,tabactivity
来源: https://codeday.me/bug/20190714/1456361.html