7

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

我在这里找到了一个接近的答案 -选项卡小部件中的背景忽略缩放。但是,我不确定在哪里添加新的 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>

主要活动:

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

那么,最好的方法是什么?

4

2 回答 2

1

看看这个,让我知道它是否对你有用。

自定义 Android 选项卡

于 2011-11-25T08:31:29.123 回答
0

我怀疑您正在努力寻找一种开箱即用的方法来执行背景图像缩放的原因是它通常不被视为一种好的做法。图像的缩放往往会引入伪影和条纹,这会使您的 UI 看起来很糟糕。例外情况是在 Imageview 中,但我认为这个类提供的缩放支持更多是为了支持照片或 Web 内容(即您的应用程序未附带的图像)之类的东西。

您的 UI 布局应以这样一种方式设计,即任何基于资源的背景图像都应预先缩放到适合设备密度/屏幕尺寸的正确分辨率。换句话说,您应该使用Supporting Multiple Screens开发指南中概述的资源文件夹命名约定来提供每个 drawable 的多个版本,以适应多种屏幕密度和大小。

然后应该对 UI 进行布局,以便可以处理设备之间屏幕尺寸的任何细微差异,而无需缩放其包含的视图的背景图像。显然我不知道您提出的 UI 是什么样子,因此很难提出任何具体建议,但通常我使用简单的背景块颜色或ShapeDrawables 来动态填充视图之间的空间。

但是,如果您真的确定要缩放背景图像,尽管上面有 :-),为什么不尝试使用 aScaleDrawable来包装现有的可绘制对象呢?

如果您知道视图和背景图像可绘制的高度和宽度,那么您可以执行以下操作:

Drawable backgroundDrawable = getResources().getDrawable(R.drawable.tab_license_off); 
tempView.setBackgroundDrawable(
    new ScaleDrawable(
        backgroundDrawable,
        Gravity.CENTER,
        tempView.getWidth() / backgroundDrawable.getIntrinsicWidth(),
        tempView.getHeight() / backgroundDrawable.getIntrinsicHeight());
于 2011-11-02T10:49:50.497 回答