android - 自定义 View 阻止显示操作栏标题?

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

我有一个带有自定义 View 的操作栏,但出于某种原因,当我将自定义 View 添加到操作栏上时,我在 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>

操作栏布局: 创建_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 也没有显示,而且我也没有在任何地方添加任何填充/边距这可能会阻止标签显示。这是一张展示正在发生的事情的照片。感谢任何帮助,谢谢 ![enter image description here ] 1

最佳答案

如上评论所述,此链接 -

Toolbar title with custom view

讨论如何保留您的标题并将自定义 View 添加到您现有的工具栏声明。

要保留标题,需要注意的主要事项是您必须确保自定义 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 中插入您喜欢的任何自定义 View 。

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

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

关于android - 自定义 View 阻止显示操作栏标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34572753/

相关文章:

android - 使用 Material 指标创建 android 工具栏

Android ConstraintLayout - 将一个 View 放在另一个 View 之上

android - 如何在操作栏中设置边框底部?

android - Android 上的短信网址

android -/系统/bin/sh : adb: not found

android - 如何将元素动态添加到使用 XML 创建的 View 中

android - 如何在 Recycler View 中插入 ProgressBar 以加载更多项目?

Android Action Bar 选项卡 - 内部 fragment 事务问题

android - 如何在接到电话时停止电话铃声,但不全局静音

android - 在 Android 中,触摸/点击应用程序屏幕的 x y 原点值是多少?