gpt4 book ai didi

android - TabHost/TabWidget - 缩放背景图像?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:53 24 4
gpt4 key购买 nike

我需要缩放我的 TabWidget 背景图像,以便它们保持纵横比。
我正在使用带有 TabWidget 的 TabHost。然后我使用 setBackgroundDrawable 来设置图像。

我在这里找到了一个接近的答案 - Background in tab widget ignore scaling .但是,我不确定在哪里添加新的 Drawable 代码。 (使用 HelloTabWidget 示例,我的模块均未使用 RelativeLayout,并且我没有看到“tabcontent”的任何布局。)

我还找到了这个线程 - Android: Scale a Drawable or background image? .根据它,听起来我必须预先缩放我的图像,这违背了使它们可缩放的整个目的。

我还发现了另一个线程,其中有人对 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>

主要 Activity :

        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 图片,但它们太小了。

那么,解决这个问题的最佳方法是什么?

最佳答案

检查一下,让我知道它是否适合您。

Custom Android Tab

关于android - TabHost/TabWidget - 缩放背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4463398/

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