动作栏的高度应与所有设备兼容?

时间:2013-09-26 05:42:20

标签: android android-actionbar

我正在为操作栏使用不同的布局,并为其添加符合我要求的不同项目。然后像这样在

中的java类中调用它
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowHomeEnabled(false);

对于2或3种不同的设备看起来很好,但看起来不兼容nexus7平板电脑和xhdpi。动作条的高度很小,它给出了空的空间。我该怎么做才能管理它?我正在使用

     android:layout_width="match_parent"
     android:layout_height="48dp"

在操作栏布局中。我也使用了

           android:layout_height="wrap_content" 

但它根本不起作用。我不知道如何解决它。请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以在源位置设置操作栏的高度,而不是在资源中设置。

所以你需要的只是计算动作栏的高度:

TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

答案 1 :(得分:0)

ActionBar bar = getSupportActionBar();
Display d = getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
d.getMetrics(dm);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)myCustomView.getLayoutParams();
final int ACTION_BAR_HEIGHT = dm.heightPixels/10; // 1/10 from screen height
lp.height = ACTION_BAR_HEIGHT;
myCustomView.setLayoutParams(lp);
bar.setCustomView(myCustomView);