问题
I am attempting to show both a title, using setTitle and a custom view in my toolbar.
I am not treating it as an actionbar, instead as nothing more than a view.
I am adding both the titles 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);
The xml for my switch view is
<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 the RelativeLayout fills the entire toolbar area and the title isn't visible.
Any ideas?
回答1:
This can be solved by adding your own title (and subtitle, if you need it) text views inside the toolbar alongside your custom view.
But perhaps a better way is to use a second toolbar nested inside the first. That way, all formatting is taken care of for 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>
回答2:
If you are not using the Toolbar as an ActionBar, then maybe you could just use a layout.
I used a LinearLayout instead of a Toolbar.
What advantages is Toolbar giving you?
来源:https://stackoverflow.com/questions/28771219/toolbar-title-with-custom-view