【问题标题】:Unable to Change ActionBar Size无法更改操作栏大小
【发布时间】:2013-10-03 10:53:05
【问题描述】:

我正在尝试更改ActionBar的高度,我使用的代码如下所示

MainActivity:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    View view = View.inflate(getApplicationContext(),
            R.layout.actionbarview, null);
    actionBar.setCustomView(view);
}

}

值/样式.xml:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<!-- general styles for the action bar -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="actionBarSize">700dp</item>
</style>

Values-v11/styles.xml

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<!-- general styles for the action bar -->
<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
     <item name="android:actionBarSize">700dp</item>
</style>

actionbarview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:background="#555333"
android:orientation="horizontal" >
<Button
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true" />
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Title mswg" />
   <Button
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" />
  </RelativeLayout>`

问题不是高度不变。我的目标sdk是17。

提前致谢!

【问题讨论】:

  • actionbarview布局的post code

标签: android android-layout android-actionbar


【解决方案1】:

看起来和你的代码很相似,很奇怪……我很幸运能用这种样式(值):

<style name="player" parent="@style/Theme.AppCompat">
    <item name="actionBarSize">90dp</item>
    <item name="actionBarStyle">@style/player.ActionBarStyle</item>
</style>

<style name="player.ActionBarStyle" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@drawable/ab_bgr</item>
</style>

值-v11

<style name="player" parent="@style/Theme.AppCompat">
    <item name="android:actionBarSize">90dp</item>
    <item name="android:actionBarStyle">@style/player.ActionBarStyle</item>
</style>

<style name="player.ActionBarStyle" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@drawable/ab_bgr</item>
</style>

ActionBarActivity

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
....

View customView = activity.getLayoutInflater().inflate(R.layout.player_ab,null);

    ActionBar actionBar = getSupportActionBar();
    //float scale = activity.getResources().getDisplayMetrics().density;

    actionBar.setCustomView(customView, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));//(int) (90*scale + 0.5f)));
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

【讨论】:

    【解决方案2】:

    试试这样-

    values/dimens.xml-

    <resources>
         <dimen name="MyActionBar">100dp</dimen>
    </resources>
    

    现在你可以像这样使用这些维度了-

    <style [...]>
         <item name="android:actionBarSize">@dimen/MyActionBar</item>
    </style> 
    

    另一种最简单的方法-

    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
            <item name="android:actionBarSize">100dp</item>
            <item name="actionBarSize">100dp</item>
    </style> 
    

    【讨论】:

    • @EmelElias 你在我的代码中写的是 700dp 而不是 100dp?
    • @EmelElias values and values-v11 用于纵向和横向模式?
    • values 和 values-11 都用于纵向。我使用了建议的 100dp。
    • 我改成200dp还是不行
    • @EmelElias 我认为我们主要使用了 2 种不同样式的 xml,一种用于纵向,一种用于横向。所以只创建一个维度和一个样式 xml。
    【解决方案3】:

    试试这个...

    <style name="Theme.MyTheme" parent="@style/Theme.AppCompat.Light">
       <item name="actionBarStyle">@style/actionBarHeight</item>
       <item name="android:actionBarStyle">@style/actionBarHeight</item>
    </style>
    <style name="actionBarHeight" parent="@style/Widget.AppCompat.ActionBar">
         <item name="android:height">60dp</item>
     </style>
    

    【讨论】:

      【解决方案4】:

      在我更改之前,提供的解决方案对我不起作用

      android:height
      

      简单

      height
      

      因为 AppCompat 不是系统的一部分。

      【讨论】:

        【解决方案5】:

        res/values/styles.xml 上添加这个:

        <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
            <item name="actionBarStyle">@style/MyActionBar</item>
        </style>
        
        <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
            <item name="height">55dp</item>
        </style>
        

        res/values-v11/styles.xmlres/values-v14/styles.xml 上添加:

        <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light">
            <item name="android:actionBarStyle">@style/MyActionBar</item>
        </style>
        
        <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
            <item name="android:height">55dp</item>
        </style>
        

        【讨论】:

          猜你喜欢
          • 2017-01-25
          • 1970-01-01
          • 2023-03-17
          • 2015-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多