23

私は最近タブバのカスタムレイアウトとディメンションが必要なAndroidアプリを開発しています。私がこれまでやったやり方は、Jake WhartonのActionBarSherlockライブラリを使ってHoneyCombの前のバージョンをサポートし、appBarSizeスタイルアイテムを編集するスタイルをアプリケーションに適用することです。Android JellyBeanのアクションバーの高さを変更しました

今、私はギャラクシーS3(それにジェリービーンズ付き)でアプリをテストしていて、タブバーの高さはactionBarSizeの値に応じてもう変化しません。

だから私はJellyBeanのコードを探し始めました、と私はActionBarPolicyクラスでこのメソッドを見つけた:

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; 
    } 

私はこの方法で収集するものから、JellyBeanがTabBarの高さを制限しているようです、アプリがポートレートモードのときは、アクションバーの高さをactionBarSizeに設定しても、タブバーの高さを「action_bar_stacked_max_height」ディメンション値(4.1の/values/dimen.xmlファイルの48dp)に設定します。

このディメンション値を、自分のdimen.xmlファイルで自分の値に設定することで上書きしようとしましたが、運がなかった。それはうまくいかなかった。

私の質問:

君たちは私が「action_bar_stacked_max_height」DIMEN値を上書きすることができる方法を知っていますか?

ありがとうございます!

+0

私も同じ問題があります。あなたがこれで見つけた解決策はありますか? – user936414

答えて

1

、意図的に行われていたように私には思えますなぜか分からない。いくつかの変数は、すでに述べたように、埋め込みタブの高さは、ActionBarPolicy.javaに48dipに制限されbools.xml

<bool name="action_bar_embed_tabs">true</bool> 
<bool name="action_bar_embed_tabs_pre_jb">false</bool> 

にあります。そのため、こうした動作はAndroid JellyBean以上でしか見られません。私はいくつかのJavaの反射を作るよりも良い解決策を見つけることができません。だからここにあなたのonCreateにコード

private void hackJBPolicy() { 
    View container = findScrollingTabContainer(); 
    if (container == null) return; 

    try { 
     int height = getResources().getDimensionPixelSize(R.dimen.action_bar_height); 
     Method method = container.getClass() 
       .getDeclaredMethod("setContentHeight", Integer.TYPE); 
     method.invoke(container, height); 
    } catch (NoSuchMethodException e) { 
     Log.e(LOG_TAG, e.getLocalizedMessage(), e); 
    } catch (IllegalArgumentException e) { 
     Log.e(LOG_TAG, e.getLocalizedMessage(), e); 
    } catch (IllegalAccessException e) { 
     Log.e(LOG_TAG, e.getLocalizedMessage(), e); 
    } catch (InvocationTargetException e) { 
     Log.e(LOG_TAG, e.getLocalizedMessage(), e); 
    } 
} 

private View findScrollingTabContainer() { 
    View decor = getWindow().getDecorView(); 
    int containerId = getResources().getIdentifier("action_bar_container", "id", "android"); 

    // check if appcompat library is used 
    if (containerId == 0) { 
     containerId = R.id.action_bar_container; 
    } 

    FrameLayout container = (FrameLayout) decor.findViewById(containerId); 

    for (int i = 0; i < container.getChildCount(); i++) { 
     View scrolling = container.getChildAt(container.getChildCount() - 1); 
     String simpleName = scrolling.getClass().getSimpleName(); 
     if (simpleName.equals("ScrollingTabContainerView")) return scrolling; 
    } 

    return null; 
} 

使用方法hackJBPolicy()です。 appcompatライブラリからActionBarを使用したことに注目してください。この回避策を使用してサンプルプロジェクトにlinkを追加します。

結局のところ、uiデザインがguidlinesから少し離れていると、タブモードでActionBarを使用するのではなく、画面の上部に合わせてカスタムビューを作成する方が簡単になるようです。

+0

サポートからではなく標準のアクションバーを使用して実装する方法を教えてくださいとしょうかん? R.id.action_bar_containerという名前のリソースが見つかりません。 – user2302510

+0

私はこれが非常に遅いコメントであることを知っていますが、私はst_の答えを受け入れるつもりですが、それは反射を使用しています(理論的にはアプリケーションを少し遅くします)。しかし、彼は "JB政策"を過ぎて邪魔するほどの情報を提供している。結局、私は自分のカスタムタブバーを開発しました。 – edy

26

はそうのように、あなたが使用しているテーマの下android:actionBarSizeactionBarSizeを入れてみてください:

<style name="Theme.white_style" parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
     <item name="android:actionBarSize">55dp</item> 
     <item name="actionBarSize">55dp</item> 
</style> 
+6

これはJellyBeanでは機能しません。 – Tomik

+1

これはJellybeanで機能します。 Nexus 4でこれを確認しました – Anton

+4

メインアクションバーの高さを変更しますが、タブバーの高さには影響しません(Android 4.2.2では不可)。 – Tomik

9

は、次のようにandroid:heightを使用してみてください:

<style name="AppActionBar"> 
    <item name="android:height">50dp</item> 
</style> 

<style name="MainActivityStyle" parent="@android:style/Theme.Holo"> 
    <item name="android:actionBarStyle">@style/AppActionBar</item> 
</style> 
+1

はスプリットアクションバーで動作しますが、2番目の行/スタックバー(タブ)では動作しません – jfcartier

関連する問題