【问题标题】:Custom View preventing Action Bar Title from showing?自定义视图阻止操作栏标题显示?
【发布时间】:2016-01-03 02:27:24
【问题描述】:

我有一个具有自定义视图的操作栏,但由于某种原因,当我将自定义视图添加到操作栏时,我在 AndroidManifest.xml 中声明的 actionBar 标签未显示。这是我的尝试:

AndroidManifest.xml:

    <activity android:name=".com.tabs.activity.CreatePost"
        android:label="Create Post"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateVisible">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".com.tabs.activity.news_feed"/>
    </activity>

操作栏布局: create_post_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".CreatePost">

    <item
        android:id="@+id/privacy_toggle"
        android:title=""
        app:showAsAction="always"
        android:orderInCategory="1"
        android:actionLayout="@layout/privacy_toggle_layout" />

    <item
        android:id="@+id/send_post"
        android:orderInCategory="2"
        android:title="Send"
        app:showAsAction="always" />

</menu>

privacy_toggle_layout.xml

<?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">
    <RadioGroup
        android:checkedButton="@+id/offer"
        android:id="@+id/privacy_toggle"
        android:layout_width="wrap_content"
        android:layout_height="25dp"
        android:background="@drawable/pick_out_line"
        android:orientation="horizontal"
        android:layout_alignParentRight="true">

        <RadioButton
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_marginLeft="1dp"
            android:id="@+id/public_toggle"
            android:background="@drawable/toggle_widget_background"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="Public"
            android:textColor="@color/white" />

        <RadioButton
            android:layout_marginRight="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:id="@+id/private_toggle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/toggle_widget_background"
            android:button="@null"
            android:gravity="center"
            android:text="Private"
            android:checked="true"
            android:textColor="@color/white" />
    </RadioGroup>
</RelativeLayout>

CreatePost.java(我只是展示如何实例化操作栏)

private void setupActionBar(){
    Toolbar toolbar = (Toolbar) findViewById(R.id.create_post_toolbar);
    setSupportActionBar(toolbar);
    //Back bar enabled
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    //Toggle bar enabled
    ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.privacy_toggle_layout);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
    final RadioGroup privacyToggle = (RadioGroup) findViewById(R.id.privacy_toggle);
    final RadioButton publicToggle = (RadioButton) findViewById(R.id.public_toggle);
    final RadioButton privateToggle = (RadioButton) findViewById(R.id.private_toggle);

    privateToggle.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));

    //Set listener for clicking on toggle
    privacyToggle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == R.id.public_toggle){
                System.out.println("Toggled public");
            }
            else {
                System.out.println("Toggled private");
            }
        }
    });
}

即使我有ActionBar.DISPLAY_SHOW_TITLE,AndroidManifest.xml 中声明的label 也没有显示,而且我也没有添加任何可能阻止标签显示的填充/边距。这是一张照片,显示正在发生的事情。感谢您提供任何帮助,谢谢]1

【问题讨论】:

  • getSupportActionBar().setDisplayShowCustomEnabled(true); ,将此行添加到您的代码中并检查。
  • 您希望标题显示的位置

标签: android android-layout android-actionbar android-custom-view


【解决方案1】:

如上面的 cmets 中所述,此链接 -

Toolbar title with custom view

讨论保留标题和将自定义视图添加到现有工具栏声明的方法。

为了保留标题,需要注意的主要一点是,您必须确保自定义视图不会妨碍/覆盖您现有的标题。

例如一个toolbar.axml -

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/White"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_gravity="right"
    android:elevation="4dp">

    <RelativeLayout
        android:id="@+id/toolbar_customview"
        android:layout_width="50dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>

会在工具栏的 RHS 上给你一个相对布局,让标题显示。然后,您可以 FindViewById,然后根据需要在 Activity / Fragment 中插入您喜欢的任何自定义视图。

显然,您必须调整宽度以保留标题并留出空间来添加自定义详细信息。

如果您只想要一个真正自定义的设计(就像您一样,标准标题的空间很小),那么您最好自己为标题添加一个文本视图,并确保为该活动设置它而不是 SetTitle。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2013-12-04
    • 1970-01-01
    相关资源
    最近更新 更多