【问题标题】:Android customized default title bar is not showing in 4.x devicesAndroid 自定义默认标题栏未在 4.x 设备中显示
【发布时间】:2013-03-21 20:33:59
【问题描述】:

我自定义了默认标题栏,它在 2.x、3.x 设备中按预期显示,但在 4.x 设备中没有显示。

在设备 2.2 上:

在设备 4.1.2 上:

这是onCreate里面的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView localTextView = (TextView) getWindow().findViewById(
                android.R.id.title);
        if (localTextView != null) {
            ViewParent localViewParent = localTextView.getParent();
            if ((localViewParent != null)
                    && ((localViewParent instanceof FrameLayout))) {
                View localView = ((LayoutInflater) getSystemService("layout_inflater"))
                        .inflate(R.layout.logout_button, null);
                UIImageButton localUIImageButton = (UIImageButton) localView
                        .findViewById(R.id.logoutButton);
                Rect localRect = new Rect();
                Window localWindow = getWindow();
                localWindow.getDecorView().getWindowVisibleDisplayFrame(
                        localRect);
                int i = localRect.top;
                int j = localWindow.findViewById(android.R.id.title).getTop() - i;
                PrintStream localPrintStream = System.out;
                Object[] arrayOfObject = new Object[1];
                arrayOfObject[0] = Integer.valueOf(j);
                localPrintStream.printf("%d", arrayOfObject);
                localUIImageButton.setMaxHeight(j);
                ((FrameLayout) localViewParent).addView(localView);
            }
        }
    }

这是清单代码:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我希望 4.x 设备中的标题栏与 2.x 中的类似。

【问题讨论】:

    标签: android custom-titlebar


    【解决方案1】:

    对于 Android 4.0 及更高版本,您需要自定义操作栏。见:http://developer.android.com/guide/topics/ui/actionbar.html

    类似的东西。

    ActionBar actionBar = getSupportActionBar();
    
    LayoutInflater inflator = (LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.logout_button, null); // your logout button
    actionBar.setCustomView(v);
    

    【讨论】:

    • 我同意,但我的应用程序支持从 2.2 到更高版本。动作栏是在 3.0+ 版本中引入的,所以我必须进行版本检查,然后进行相应的定制。但是我通过添加主题找到了一个简单的解决方案。
    • 我同意版本检查是丑陋的。很高兴您找到了快速解决方案!
    【解决方案2】:

    花了几个小时后,我得到了解决问题的方法。 将此android:theme=@android:style/Theme.Light 主题添加到活动是已解决的问题,现在与上面设备 2.2 屏幕截图所示相同的自定义标题栏可以正确显示在所有版本中。

    【讨论】:

      【解决方案3】:

      解决此问题的一种快速方法是将 android:targetSdkVersion 更改回预蜂窝版本。然后应用程序将使用兼容模式呈现,并且标题栏应与旧设备匹配。

      【讨论】:

      • 是的,解决方案有效并且可以快速修复。但理想情况下 android:targetSdkVersion 应该有最新版本来支持应用程序。我通过添加主题找到了一个简单的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      相关资源
      最近更新 更多