android - 具有自定义 RelativeLayout 的 ActionBarSherlock 不显示标题文本

标签 android actionbarsherlock

ActionBar 在使用相对布局时不显示标题。 当我不使用 RelativeLayout 时,项目不会右对齐。 请不要告诉我使用菜单项作为解决方案。它必须是自定义的。

我是这样设置的:

    ActionBar bar = getSupportActionBar();
    bar.setDisplayOptions(
            ActionBar.DISPLAY_HOME_AS_UP |
            ActionBar.DISPLAY_SHOW_CUSTOM |
            ActionBar.DISPLAY_SHOW_HOME |
            ActionBar.DISPLAY_SHOW_TITLE |
            ActionBar.DISPLAY_USE_LOGO);

    bar.setTitle(title);
    bar.setCustomView(actionBar);
    this.setTitle(title);

这是对齐良好,但标题未显示的情况

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true" >

        <ImageButton
            android:id="@+id/acion_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_label_add"
            style="?attr/actionButtonStyle" />

        <ImageButton
            android:id="@+id/action_prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_label_prev"
            style="?attr/actionButtonStyle" />

        <ImageButton
            android:id="@+id/action_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_label_next"
            style="?attr/actionButtonStyle" />

    </LinearLayout>

</RelativeLayout>

Screenshot

这是显示标题的布局,但项目是左对齐的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:gravity="right">

    <ImageButton
        android:id="@+id/acion_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu_label_add"
        style="?attr/actionButtonStyle" />

    <ImageButton
        android:id="@+id/action_prev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu_label_prev"
        style="?attr/actionButtonStyle" />

    <ImageButton
        android:id="@+id/action_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu_label_next"
        style="?attr/actionButtonStyle" />

</LinearLayout>

Screenshot

最佳答案

我遇到了同样的问题并找到了这个链接:https://groups.google.com/forum/?fromgroups=#!topic/actionbarsherlock/He6gst4nvR0

这对我有用:

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    ...

</LinearLayout>

代码:

import com.actionbarsherlock.app.ActionBar.LayoutParams;
...
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
getSupportActionBar().setCustomView(segmentView, lp);

关于android - 具有自定义 RelativeLayout 的 ActionBarSherlock 不显示标题文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13137325/

相关文章:

java - 更改窗口标题背景样式

android - 窗口管理器坏 token 异常

java - 设置 Android ActionBar Home/Up 按钮

android - 如何在 SherlockFragmentActivity 类中设置 actionbarsherlock 的 CustomView

java - Android 操作栏标题文本在某些手机上不可点击

android - 跳过 320 帧在主线程上做的太多

java - Android Studio项目级build.gradle错误

android - 保存 pdf 而不向用户显示打印对话屏幕

android - 带有自定义布局的操作栏显示我的图标

android - 如何在 ActionBar 中添加编辑文本字段和按钮?