Android 如何在我的应用程序中访问工具栏小部件上的文本?
我已按照本页的指导原则将工具栏添加到我的Android应用程序中: * * 以下xml代码已添加到我的布局xml文件中:Android 如何在我的应用程序中访问工具栏小部件上的文本?,android,xml,user-interface,android-support-library,Android,Xml,User Interface,Android Support Library,我已按照本页的指导原则将工具栏添加到我的Android应用程序中: * * 以下xml代码已添加到我的布局xml文件中: <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:backg
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
我的应用程序上有工具栏。但是我真的想把工具栏的“标题”移到中间。“title”是我的应用程序的名称。我想改变它的重力和外观。因此,我尝试对xml代码进行以下更改,但没有成功
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:text="MySolvingApp"
android:fontFamily="@font/cutive"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
如何访问此文本并更改其属性和外观?可以通过xml布局或代码访问它吗
谢谢你的建议我记得这在很久以前对我很有效
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Toolbar text"
android:gravity="center"/>
</android.support.v7.widget.Toolbar>
您可以添加此
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=your text/>
</android.support.v7.widget.Toolbar
我可以通过执行以下操作来创建答案:
toolbar.setTitle("");
toolbar.setBackground(new MyTitleDrawable());
在这个可绘制类中,您将创建一个绘制标题的绘画。
油漆可以根据您的喜好、文本大小、笔划宽度、字体等进行定制
确保您还创建了一个表示appTheme中颜色“colorPrimary”的int
在资源下归档,以跟上材料设计指南
color = context.getResources().getColor(R.color.colorPrimary,null);
内部onDraw方法:
//draw the title on the center of canvas
canvas.translate(canvas.getWidth()/2f , canvas.getHeight()/2f);
//Don't forget to measure your text title with a Rect object
paint.getTextBounds(title,0,title.length(),rect);
//now draw your title at the origin which is the center of the canvas.
//draw it right at 0 , 0 and take into account rect.width()
canvas.drawText(title,0 - rect.width()/2f , 0 + rect.height() / 2f , paint);
完成此操作后,您将获得一个很好的工具栏,其中的标题已完全自定义
根据您的喜好,它位于工具栏的正中央。可能是@user1506104的副本。关于该链接,我没有读太多。我正在android参考文档中查找widget工具栏V7下的任何代码,请查看下面的答案。不久前它对我有效。@user1506104我已经尝试过你的答案,但是角色>给出了问题。它表示工具栏小部件在xml文件中未关闭以下字符出现问题“>”它表示小部件未关闭
//draw the title on the center of canvas
canvas.translate(canvas.getWidth()/2f , canvas.getHeight()/2f);
//Don't forget to measure your text title with a Rect object
paint.getTextBounds(title,0,title.length(),rect);
//now draw your title at the origin which is the center of the canvas.
//draw it right at 0 , 0 and take into account rect.width()
canvas.drawText(title,0 - rect.width()/2f , 0 + rect.height() / 2f , paint);