Я хочу, чтобы иметь возможность назначать разные изображения для моих вкладок в элементе управления TabLayout в зависимости от того, выбран ли элемент или нет. Я следил за учебником на сайте Android, но они сделали пример с одним изображением и для него он работает. Но это не работает для остальных вкладок. Как я могу заставить его работать? Это мой код:Учебник по макету Android Tab?
Основная деятельность:
public class Main extends TabActivity {
private Resources res;
private TabHost tabHost;
private TabHost.TabSpec spec; // Resusable TabSpec for each tab
private Intent intent; // Reusable Intent for each tab
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The IDs in main2 should be exactly like the current ones.
setContentView(R.layout.main2);
res = getResources(); // Resource object to get Drawables
tabHost = getTabHost(); // The activity TabHost
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tab1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("artists").setIndicator("Artists",
res.getDrawable(R.drawable.ic_tab_artists_grey))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
res.getDrawable(R.drawable.ic_tab_albums_grey))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, Tab3.class);
spec = tabHost.newTabSpec("songs").setIndicator("Songs",
res.getDrawable(R.drawable.ic_tab_songs_grey))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);
}
}
Основной XML:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Селектор для первой вкладке:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/ic_tab_artists_grey"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/ic_tab_artists_white"/>
</selector>
Селектор для второго tab:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/ic_tab_songs_grey"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/ic_tab_songs_white"/>
</selector>
и т. Д. Мои изображения называются ic_tab_songs_white, ic_tab_songs_grey, ic_tab_albums_white, ic_tab_albums_grey, ic_tab_artists_white, ic_tab_artists_grey.
Да, это правильный ответ. Спасибо :) –