gpt4 book ai didi

Android UI TabActivity 问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:59 25 4
gpt4 key购买 nike

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

对于背景图像(应用程序背景)...我在 setContentView(布局)中设置图像...通过添加此行,我收到运行时异常...

如果我在子 Activity 中设置这个背景..我不会得到背景来填充整个应用程序背景..知道有什么选择吗?

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 的方法。然后像这样膨胀 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 UI TabActivity 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5567532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com