LoginSignup
17
16

More than 5 years have passed since last update.

targetSdkVersionでUIが切り替わる理由

Last updated at Posted at 2015-08-12

build.gradleで指定するtargetSdkが変わるとテーマが切り替わったり、バーチャルメニューキーが表示されたりします。
Android 4.4端末で5.1のAPIレベルを指定すると以下のように今風のUIが表示されています

image

しかし、Android 4.4端末で2.2のAPIレベルを指定すると以下のようにバーチャルのメニューキーが表示され、また古いUIが表示されます。また上はActionBarではなく、細いバーが表示されています。

image

そこで、仕組みがわからなくてもやもやしていました。

targetSdkVersionによって切り替える仕組みなどがあるわけではなく、
targetSdkVersionは普通にAndroidの中のプログラムで利用するためのもののようです。。

ちょっと探してみたのは以下になります(違う場所を見ているなどがありましたらコメントお願いします。)

メニューキーの切り替わるのはなぜ?

        final Context context = getContext();
        final int targetSdk = context.getApplicationInfo().targetSdkVersion;
        final boolean targetPreHoneycomb = targetSdk < android.os.Build.VERSION_CODES.HONEYCOMB;
        if (targetPreHoneycomb || (targetPreIcs && targetHcNeedsOptions && noActionBar)) {
            setNeedsMenuKey(WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE);
        } else {
            setNeedsMenuKey(WindowManager.LayoutParams.NEEDS_MENU_SET_FALSE);
        }

テーマが変わるのはなぜ?

   @Override public Resources.Theme getTheme() {
        if (mTheme != null) {
            return mTheme;
        }

        mThemeResource = Resources.selectDefaultTheme(mThemeResource,
                getApplicationInfo().targetSdkVersion);
        initializeTheme();

        return mTheme;
    }

ActionBarの高さが違うのはなぜ?

    public boolean hasEmbeddedTabs() {
        final int targetSdk = mContext.getApplicationInfo().targetSdkVersion;
        if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) {
            return mContext.getResources().getBoolean(R.bool.action_bar_embed_tabs);
        }

        // The embedded tabs policy changed in Jellybean; give older apps the old policy
        // so they get what they expect.
        return mContext.getResources().getBoolean(R.bool.action_bar_embed_tabs_pre_jb);
    }

    public int getTabContainerHeight() {
        TypedArray a = mContext.obtainStyledAttributes(null, R.styleable.ActionBar,
                com.android.internal.R.attr.actionBarStyle, 0);
        int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
        Resources r = mContext.getResources();
        if (!hasEmbeddedTabs()) {
            // Stacked tabs; limit the height
            height = Math.min(height,
                    r.getDimensionPixelSize(R.dimen.action_bar_stacked_max_height));
        }
        a.recycle();
        return height;
    }
17
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16