android - 标题未在添加了自定义 View 的 Android 工具栏小部件中居中

标签 android android-fragments android-activity material-design


我正在尝试创建一个可穿戴应用程序,我需要在工具栏中添加自定义布局,其中包含相对布局中的 TextView,属性 android:layout_center_in_parent 设置为 true。但问题是 TextView 没有出现在工具栏的中心。任何人都可以帮助我如何在 ToolBar 的中心显示 TextView 。
我使用下面的代码为工具栏和我的布局添加自定义 View ,如下所示:

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);


我的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>

最佳答案

试试这个 .. 只需更改 TextView xml 代码。如果您将 android:layout_width="wrap_content" 更改为 android:layout_width="match_parent" 那么它将水平居中..

<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>

如果您将 android:layout_height="wrap_content" 更改为 match_parent 那么您的 textview 也将垂直居中 ...

关于android - 标题未在添加了自定义 View 的 Android 工具栏小部件中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33227591/

相关文章:

android - 如何防止 Android Telnet 客户端 session 丢失

java - 字符串设置为 ArrayList?

java - 回调方法响应后无法获取整数值

android - 单击 ListView 项目时打开一个新 fragment

java - 无法从 OnClickListener 启动 Activity "method startActivity(Intent, int) is undefined for the type"

Android:按下按钮时保持通知打开

android - getActivity() 在 Fragment 函数中返回 null

java - 开始时的其他 Activity

fragment 中未显示 Android EditText 键盘

android - 如何检查 Activity 是否启用?