Android 自定义默认标题栏未在 4.x 设备中显示

标签 android custom-titlebar

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

在设备 2.2 上:
enter image description here

在设备 4.1.2 上:
enter image description here

这是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);
            }
        }
    }

这是 list 代码:

<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 4.0及以上版本需要自定义action bar。参见: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);

关于Android 自定义默认标题栏未在 4.x 设备中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15557526/

相关文章:

android - 自定义 Gradle 日志记录

java - Json Jackson 不反序列化内部对象(属性上为 null)

java - 查找第二个最接近的值总是返回最后一个最接近的值

java - 更改微调器标题栏样式

windows - Gnu emacs 窗口标题默认

c - 在 GTK 的标题栏中添加一个工具按钮

java - 应用程序在主线程上做太多工作

Android:如何使用Retrofit获取 session ID?

android - 将图像添加到 Android 中的自定义窗口标题栏