Why are the images in WidgetTab not shown if minSdkVersion>10?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 20:15:28

问题


I have been attempting to create a simple TabActivity with 3 Tabs. All works except if I put android:minSdkVersion="11" in the Manifest file, the icons are not shown. If I set `minSdkVersion="10", all is well.

I have looked high and low, but I have not been able to determine what is wrong.
I have put the same images in the seemingly appropriate resource directories:

res/drawable-hdpi-v5
res/drawable-ldpi-v5
res/drawable-mdpi-v5
res/drawable-xhdpi-v5

And the the code is simple:

import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;

public class Review extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TabHost tabs = getTabHost();
        getLayoutInflater().inflate(R.layout.main, 
                                    tabs.getTabContentView(), true);
        Resources resources=getResources();
        Log.d("testing", String.format("icon: %d.%d",
                    resources.getDrawable(R.drawable.review).getIntrinsicWidth(), 
            resources.getDrawable(R.drawable.review).getIntrinsicHeight()));
        TabHost.TabSpec details = tabs.newTabSpec("review"). setContent(R.id.review). 
                setIndicator(getString(R.string.review), 
                        resources.getDrawable(R.drawable.review));        
        TabHost.TabSpec gallery=tabs.newTabSpec("gallery").setContent(R.id.photos)
                .setIndicator(getString(R.string.gallery), 
                        resources.getDrawable(R.drawable.photos));
        TabHost.TabSpec reservation=tabs.newTabSpec("reservation").
             setContent(R.id.reservation)
                .setIndicator(getString(R.string.reservation), 
                        resources.getDrawable(R.drawable.reservation));
        tabs.addTab(details);
        tabs.addTab(gallery);
        tabs.addTab(reservation);    
    }
}

In digging into this, the only difference I can see internally under android 2.0 vs 3.0 is that Android uses a RelativeLayout instead of a LinearLayout in the 2.0 implementation.

Just to be certain that the icons images are being found, Log.d of above shows:

icon: 32.32 as it should.

Why does this shift from android 2.0 to 3.0 do this???? I am hopeful that someone else has run into this and it is obvious. Thanks very much for your help!

-- UPDATE:

I discovered today, as I looked more closely at what is actually happening when this code is built for android 3.0+, I learned that the ImageView's that come about when SetIndeicator(string, drawable) is called for each TabSpec, are actually never set and are actually NULL (ImageView.mDrawable==null) and INVISBLE.

If I force set those drawables to be set, and call ImageView.setVisiblity(View.VISIBLE) then they show up. However under android 2.0 they appear stacked with the image above and the text below as in:

<image>
<text>

Under android 3.0 they appear (when forced as above) side by side as in:

<image><text>

Thus it seems that things have changed a great deal and I need to investigate the changes for android 3.0 more carefully.

Stay tuned for more...

-- Final UPDATE:

Ultimately, I abandoned this avenue and decided that this style of doing things changed and is perhaps now depreciated and there are other better ways to do this and the icons are a bit old style.


回答1:


strange, this solves the problem

    //bmOptions.inSampleSize = 1;
    //or better
    bmOptions.inScaled = false;

more at: https://stackoverflow.com/a/12088287/1320686



来源:https://stackoverflow.com/questions/10747507/why-are-the-images-in-widgettab-not-shown-if-minsdkversion10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!