I am trying to create a wearable app where I need to add a custom layout to toolbar ,which contains TextView in relative layout with attribute android:layout_center_in_parent set to true.But the problem is the TextView does not appear in center of ToolBar. Can anyone help me how to display the TextView in center for ToolBar .
I used the used the below code to add customview for Toolbar and my layouts as below :
android.support.v7.widget.Toolbar toolbar = ( android.support.v7.widget.Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("");
// toolbar.setContentInsetsAbsolute(0,0);
setSupportActionBar(toolbar);
LayoutInflater mInflater= LayoutInflater.from(getApplicationContext());
View mCustomView = mInflater.inflate(R.layout.toolbar_custom_view, null);
toolbar.addView(mCustomView);
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout
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"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
app:layout_box="all">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/framelayout"
>
</FrameLayout>
</LinearLayout>
</android.support.wearable.view.BoxInsetLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="30dp"
xmlns:app="http://schemas.android.com/tools"
android:background="?attr/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>
</android.support.v7.widget.Toolbar>
toolbar_custom_view.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Patient List"
android:id="@+id/title"
android:layot_center_in_parent="true"
android:gravity="center"
/>
</RelativeLayout>
try this .. jsut change TextView xml code. if you change android:layout_width="wrap_content"
to android:layout_width="match_parent"
then it will be horizontally center..
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Patient List"
android:id="@+id/title"
android:gravity="center"/>
</RelativeLayout>
if you change android:layout_height="wrap_content"
to match_parent
then your textview also will be vertically center ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.