Мне нужно масштабировать фоновые изображения TabWidget, чтобы они сохраняли соотношение сторон.
Я использую TabHost с TabWidget. Затем я использую setBackgroundDrawable для установки изображений.
Я нашел близкий ответ здесь - Фон в виджете вкладки игнорирует масштабирование. Однако я не уверен, куда добавить новый код Drawable. (Работая с примером HelloTabWidget, ни один из моих модулей не использует RelativeLayout, и я не вижу никакого макета для «контента вкладок».)
Я также нашел эту ветку - Android: масштабирование Drawable или фонового изображения?. Согласно этому, похоже, что мне придется предварительно масштабировать свои изображения, что противоречит самой цели сделать их масштабируемыми.
Я также нашел еще один поток, в котором кто-то создал подкласс класса Drawable, чтобы он либо не масштабировался, либо масштабировался правильно. Я не могу найти его сейчас, но кажется, что это МНОГО, когда вы должны просто сделать что-то простое, например, mTab.setScaleType(centerInside).
Вот мой код:
основной.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"
android:background="@drawable/main_background">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</TabHost>
основная деятельность:
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
TabHost changedTabHost = getTabHost();
TabWidget changedTabWidget = getTabWidget();
View changedView = changedTabHost.getTabWidget().getChildAt(0);
public void onTabChanged(String tabId) {
int selectedTab = changedTabHost.getCurrentTab();
TabWidget tw = getTabWidget();
if(selectedTab == 0) {
//setTitle("Missions Timeline");
View tempView = tabHost.getTabWidget().getChildAt(0);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_on));
tempView = tabHost.getTabWidget().getChildAt(1);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_off));
tempView = tabHost.getTabWidget().getChildAt(2);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off));
tempView = tabHost.getTabWidget().getChildAt(3);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off));
tempView = tabHost.getTabWidget().getChildAt(4);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off));
//ImageView iv = (ImageView)tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.icon);
//iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_timeline_on));
//iv = (ImageView)tabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.icon);
//iv.setImageDrawable(getResources().getDrawable(R.drawable.tab_map_off));
} else if (selectedTab == 1) {
//setTitle("Spinoffs Around You");
View tempView = tabHost.getTabWidget().getChildAt(0);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_timeline_off));
tempView = tabHost.getTabWidget().getChildAt(1);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_map_on));
tempView = tabHost.getTabWidget().getChildAt(2);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_search_off));
tempView = tabHost.getTabWidget().getChildAt(3);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_news_off));
tempView = tabHost.getTabWidget().getChildAt(4);
tempView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_license_off));
}
Я также пробовал изображения 9patch, но они оказались слишком маленькими.
Итак, как лучше всего это сделать?