ActionBarSherlock定制RelativeLayout的不显示标题文本文本、标题、ActionBarSherlock、RelativeLayout

2023-09-03 22:32:36 作者:卡哇伊宝宝

在动作条不使用相对布局时显示的标题。 当我不使用RelativeLayout的,该项目不靠右侧。 请不要告诉我使用的菜单项作为解决方案。它必须是定制的。

The ActionBar does not show title when using relative layout. And when I not use the RelativeLayout, the items are not aligned to right. Please don't tell me to use menu items as the solution. It must be custom.

下面是我的设置:

    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);

这是当对齐是好的,但标题没有显示

This is when the align is good, but title is not shown

<?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>

截图

这里来,其中的标题显示的布局,但项目是对齐到左

And here comes the layout where the title is shown, but the items are aligned to left

<?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>

截图

推荐答案

我有同样的问题,发现此链接:https://groups.google.com/forum/?fromgroups=#!topic/actionbarsherlock/He6gst4nvR0

I had the same problem and found this link: 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>

code:

code:

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);