web-dev-qa-db-ja.com

ピクセル単位でのActionBarのサイズは?

正しい背景画像を適用するには、ActionBarの正確なサイズをピクセル単位で知る必要があります。

244
Eugene

ActionBarの高さをXMLで取得するには、次のようにします。

?android:attr/actionBarSize

actionBarSherlockまたはAppCompatユーザーの場合は、これを使用してください。

?attr/actionBarSize

実行時にこの値が必要な場合は、これを使用してください。

final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
                    new int[] { Android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();

これが定義されている場所を理解する必要がある場合

  1. 属性名自体はプラットフォームの / res/values/attrs.xml で定義されています。
  2. プラットフォームの themes.xml はこの属性を選び、それに値を割り当てます。
  3. 手順2で割り当てられる値は、プラットフォームの さまざまなdimens.xmlファイル で定義されているさまざまなデバイスサイズによって異なります。 core/res/res/values-sw600dp/dimens.xml
524
AZ13

Android 3.2のframework-res.apkのコンパイル済みソースから、res/values/styles.xmlには以下が含まれます。

<style name="Theme.Holo">
    <!-- ... -->
    <item name="actionBarSize">56.0dip</item>
    <!-- ... -->
</style>

3.0と3.1は同じように見えます(少なくともAOSPから)...

54
Jake Wharton

アクションバーの実際の高さを取得するには、実行時に属性actionBarSizeを解決する必要があります。

TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(Android.R.attr.actionBarSize, tv, true);
int actionBarHeight = getResources().getDimensionPixelSize(tv.resourceId);
43
David Boho

ハニカムサンプルの1つは?android:attr/actionBarSizeを参照しています

33

私はこれらの高さをICS以前の互換性アプリで適切に再現し、 framework core source に掘り下げる必要がありました。上記の答えはどちらも正しいものです。

基本的には修飾子を使うことになります。高さは "action_bar_default_height"という次元で定義されます

デフォルトでは48dipに定義されています。しかし-landでは40dip、sw600dpでは56dipです。

20
Manfred Moser

最近のv7 appcompatサポートパッケージの互換性ActionBarを使用している場合は、次のようにして高さを取得できます。

@dimen/abc_action_bar_default_height

ドキュメンテーション

17
flawedmatrix

新しい v7サポートライブラリ (21.0.0)では、R.dimenの名前が @ dimen/abc_action_bar_default_height _materialに変更されました。 - ) .

以前のバージョンのサポートライブラリからアップグレードするときは、アクションバーの高さとしてその値を使うべきです。

16
MrMaffen

ActionBarSherlockを使用している場合は、次のようにして高さを取得できます。

@dimen/abs__action_bar_default_height
9
JesperB

@ AZ13の答えは良いのですが、 Androidデザインガイドライン に従って、ActionBarは 少なくとも48dpの高さ であるべきです。

4
aaronsnoswell

> 441dpi> 1080 x 1920> getResources()でアクションバーの高さを取得するgetDimensionPixelSize 144ピクセル.

公式px = dp x(dpi/160)を使用して、私は441dpiを使用していましたが、私のデバイスは
カテゴリ480dpiのだからそれを置くことは結果を確認します。

0
Muhammad

Class Summary から始めるのが普通です。 getHeight()メソッドで十分なはずです。

編集:

あなたが幅を必要とするならば、それはスクリーンの幅であるべきです(正しい?)そしてそれは集められることができます これのように

0
Matt

私はこのように自分自身のためにやった、このヘルパーメソッドは誰かにとって便利になるべきです:

private static final int[] RES_IDS_ACTION_BAR_SIZE = {R.attr.actionBarSize};

/**
 * Calculates the Action Bar height in pixels.
 */
public static int calculateActionBarSize(Context context) {
    if (context == null) {
        return 0;
    }

    Resources.Theme curTheme = context.getTheme();
    if (curTheme == null) {
        return 0;
    }

    TypedArray att = curTheme.obtainStyledAttributes(RES_IDS_ACTION_BAR_SIZE);
    if (att == null) {
        return 0;
    }

    float size = att.getDimension(0, 0);
    att.recycle();
    return (int) size;
}
0
Min2