如何更改 Android 中选项卡的背景颜色?
我正在使用选项卡。我想更改选项卡的背景颜色或主题。当我单击选项卡时,该选项卡以默认颜色显示为灰色,但我想以自定义颜色显示该选项卡。
这是我的代码:
<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"
android:orientation="vertical"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:background="#FF0000"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
类文件
public class AndroidtabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// View title = getWindow().findViewById(android.R.id.title);
// View titleBar = (View) title.getParent();
// titleBar.setBackgroundResource(R.drawable.top_bar2);
Resources res = getResources();
TabHost tabHost = getTabHost();
//tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
//tabHost.setBackgroundResource(R.drawable.pink);
TabHost.TabSpec spec;
//tabHost.getTabWidget().setDividerDrawable(R.drawable.top_bar);
Intent in;
in = new Intent().setClass(this, MainActivity.class);
spec=tabHost.newTabSpec("calc").setIndicator("calculate").setContent(in);
tabHost.addTab(spec);
in = new Intent().setClass(this, TutorialZoomActivity1.class);
spec=tabHost.newTabSpec("help").setIndicator("help").setContent(in);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
// spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.flash));
}
}
请告诉我如何更改选项卡的背景颜色。
i have working with tabs. i want to change the background color or theme for tab. when i click on the tab appears in default color as grey but i want to display that tab in custom color.
here my code:
<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"
android:orientation="vertical"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:background="#FF0000"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
</LinearLayout>
</TabHost>
class file is
public class AndroidtabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// View title = getWindow().findViewById(android.R.id.title);
// View titleBar = (View) title.getParent();
// titleBar.setBackgroundResource(R.drawable.top_bar2);
Resources res = getResources();
TabHost tabHost = getTabHost();
//tabHost = (TabHost) findViewById(R.id.tabhost);
tabHost.setup();
//tabHost.setBackgroundResource(R.drawable.pink);
TabHost.TabSpec spec;
//tabHost.getTabWidget().setDividerDrawable(R.drawable.top_bar);
Intent in;
in = new Intent().setClass(this, MainActivity.class);
spec=tabHost.newTabSpec("calc").setIndicator("calculate").setContent(in);
tabHost.addTab(spec);
in = new Intent().setClass(this, TutorialZoomActivity1.class);
spec=tabHost.newTabSpec("help").setIndicator("help").setContent(in);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
// spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.flash));
}
}
please tell me how can i change the background color for tabs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该创建自定义选项卡。
,将 android:background 设置为可绘制对象中的另一个 xml 文件 (@drawable/tab_selector)
祝你好运
Maybe you should create your custom tab.
good luck
您可以通过以下方式更改选项卡背景颜色
You can change tab background color by