[英]Change tab indicator color in actionbar
我知道這個問題之前已被問過多次,但我嘗試了所有解決方案,但沒有任何工作。 所以我決定遵循官方文檔: https : //developer.android.com/training/basics/actionbar/styling.html
我做了文件themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@drawable/actionbar_background</item>
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="@android:style/Widget.Holo.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">@drawable/actionbar_tab_indicator</item>
</style>
</resources>
actionbar_background只是簡單的形狀,帶有實心填充,並且正常工作。
但是指標仍為藍色:
actionbar_tab_indicator - 這是從http://jgilfelt.github.io/android-actionbarstylegenerator/生成的文件。我還將生成的圖像復制粘貼到可繪制文件夾。 第一個問題是9補丁文件不知何故錯了,但我設法手動修復它們。
我在AndroidManifest.xml中設置主題:android:theme =“@ style / CustomActionBarTheme”
我也嘗試過以編程方式更改顏色的解決方案,但它不能按預期工作。 而不是小矩形,它被拉伸以適應整個標簽。
在最新的android工作室中工作的任何解決方案(編程或xml)都會很棒。 但它必須看起來像原始標簽,但與其他顏色 - 在我的情況下紅色。
問候
我總是發現使用Android Asset Studio中的工具最簡單,因為我沒有特別的圖形傾向。 特別是這個工具與你想要完成的工作非常相關: http : //jgilfelt.github.io/android-actionbarstylegenerator/ 。 可以在http://romannurik.github.io/AndroidAssetStudio/找到全套工具。 只需選擇所需的顏色/樣式,下載zip文件,然后將文件復制/粘貼到項目中即可。 然后在AndroidManifest.xml文件中,引用您在包含ActionBar的活動中的生成器(您在網站上填寫的第一個文本框)中選擇的“樣式名稱”。 因此,例如,如果您在生成器中調用了樣式“Custom_action_bar”,則會將其添加到您的活動中,如下所示:
<activity
android:name="com.company.appname.ActivityName"
android:label="@string/somename"
android:theme="@style/Theme.Custom_action_bar">
<!--the above line is all you need to add-->
</activity>
你能試一下嗎:
<style name="MyActionBarTabs"
parent="@android:style/Widget.Holo.Light.ActionBar.TabView">
<item name="android:background">@drawable/actionbar_tab_indicator</item>
</style>
因為你的parent="@android:style/Theme.Holo.Light.DarkActionBar"
for CustomActionBarTheme
首先,在drawable文件夾中定義一個選擇器(tab_indicator.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/background_tab_selected" />
<item android:state_selected="false" android:drawable="@drawable/background_tab_unselected" />
</selector>
@ drawable / background_tab_selected和@ drawable / background_tab_unselected是九個補丁圖像,分別表示選定和未選擇的選項卡狀態。
注意: background_tab_selected九補丁圖像的可縮放區域應僅包含與正常背景匹配的像素。 像這樣: (我的意思是左邊和前九個補丁指標)
然后在你的活動的onCreate()中,找到TabWidget對象並設置它們的背景:
final TabWidget tabs = (TabWidget) findViewById(android.R.id.tabs);
tabs.getChildTabViewAt(0).setBackgroundResource(R.drawable.tab_indicator);
tabs.getChildTabViewAt(1).setBackgroundResource(R.drawable.tab_indicator);
tabs.getChildTabViewAt(2).setBackgroundResource(R.drawable.tab_indicator);
tabs.setDividerDrawable(R.drawable.tab_divider);
你也可以通過setDividerDrawable()方法設置divider drawable。
希望有所幫助。
更改actionBar選項卡指示器顏色很困難,您可以使用SlidingTabLayout來動態更改指示器顏色,也可以更改指示器的高度。
要獲取SlidingTabLayout類(&widget),請在android studio中導入示例項目:
在slidingTabLayout中,有各種選項可以自定義選項卡,指示器及其文本。
在您的項目中使用它,享受!
在.xml文件中,您可以使用以下代碼更改tabIndicator的高度。
<android.support.design.widget.TabLayout`
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="62dp"
android:background="#001e8b"
android:elevation="6dp"
app:tabSelectedTextColor="@android:color/white"
app:tabIndicatorHeight="5dp"
app:tabIndicatorColor="@android:color/white"
app:tabTextColor="@android:color/darker_gray"`
/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.