Custom view toolbar title

I am trying to show both the title using setTitle and the custom view in my toolbar.

I do not see it as an action bar, and not as something more.

I add headers and custom view in Java

toolbar = (Toolbar) view.findViewById(R.id.toolbar); if (title != null) { toolbar.setTitle(title); toolbar.setTitleTextColor(getResources().getColor(android.R.color.white)); } if (subtitle != null) { toolbar.setSubtitle(subtitle); toolbar.setSubtitleTextColor(getResources().getColor(android.R.color.white)); } // Add switch view to toolbar View switchView = LayoutInflater.from(getActivity()) .inflate(R.layout.device_list_switch, null); toolbar.addView(switchView); 

xml for my switch view

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/holo_blue_bright"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/discoverable" android:layout_alignParentBottom="true" android:layout_alignTop="@+id/discoverable" android:layout_toLeftOf="@+id/discoverable" android:layout_toStartOf="@+id/discoverable" android:gravity="center" android:text="@string/discoverable_switch_label" android:textColor="@android:color/white" /> <Switch android:id="@+id/discoverable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginLeft="@dimen/discoverable_switch_margin_left" android:layout_marginStart="@dimen/discoverable_switch_margin_left"/> </RelativeLayout> 

What happens is that RelativeLayout fills the entire area of ​​the toolbar, and the title is not displayed.

Any ideas?

+6
source share
2 answers

This can be resolved by adding your own title (and a subtitle if you need one) as text views inside the toolbar along with your custom view.

But perhaps the best way is to use the second toolbar nested in the first. Thus, all formatting will take care of you.

 <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/topToolbar" android:layout_alignParentTop="true" android:layout_height="wrap_content" android:layout_width="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/titleToolbar" android:layout_width="wrap_content" android:layout_height="match_parent" app:contentInsetStart="0dp" app:contentInsetLeft="0dp"> </android.support.v7.widget.Toolbar> <CustomView... /> </LinearLayout> </android.support.v7.widget.Toolbar> 
0
source

If you are not using the toolbar as an ActionBar, perhaps you can just use the layout.

I used LinearLayout instead of a toolbar.

What are the benefits of a toolbar?

-1
source

Source: https://habr.com/ru/post/983119/


All Articles