自定义视图阻止动作栏标题显示?

时间:2016-01-03 02:27:25

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

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

的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也没有显示,而且我也没有添加任何可能阻止标签的填充/边距展示。这张照片展示了正在发生的事情。感谢任何帮助,感谢![enter image description here] 1

1 个答案:

答案 0 :(得分:1)

如上面评论中所述,此链接 -

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中插入您喜欢的任何自定义视图。

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

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

相关问题