Android 如何在AppCompat中以文本视图的形式获取操作栏标题?
我正在尝试将标题设置为TextView,但它不起作用:Android 如何在AppCompat中以文本视图的形式获取操作栏标题?,android,Android,我正在尝试将标题设置为TextView,但它不起作用: TextView title = (TextView) findViewById(R.id.action_bar_title); 标题为空。如何使用AppCompat以文本视图的形式获取标题?通常在我的案例中使用工具栏时,如果我对标题进行自定义操作,我只需手动放大标题视图,然后用XML设置其属性。工具栏的作用是防止类似的事情发生,这样您就可以更好地控制工具栏的外观 <android.support.v7.widget.Toolbar
TextView title = (TextView) findViewById(R.id.action_bar_title);
标题为空。如何使用AppCompat以文本视图的形式获取标题?通常在我的案例中使用工具栏时,如果我对标题进行自定义操作,我只需手动放大标题视图,然后用XML设置其属性。工具栏的作用是防止类似的事情发生,这样您就可以更好地控制工具栏的外观
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_height="@dimen/standard_vertical_increment"
android:layout_width="match_parent"
android:minHeight="@dimen/standard_vertical_increment"
android:background="@drawable/actionbar_background">
<TextView
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/Red" />
</android.support.v7.widget.Toolbar>
取自暴力似乎是一种解决方案:
private void findTextViewTitle() {
String title = "title";
ActionBar ab = getSupportActionBar();
ab.setTitle(title);
Window window = getWindow();
View decor = window.getDecorView();
ArrayList<View> views = new ArrayList<View>();
decor.findViewsWithText(views, title, View.FIND_VIEWS_WITH_TEXT);
for (View view : views) {
Log.d(TAG, "view " + view.toString());
}
TextView tvTitle = (TextView) decor.findViewById(views.get(0).getId());
tvTitle.setBackgroundColor(Color.RED);
}
private void findTextViewTitle(){
字符串title=“title”;
ActionBar ab=getSupportActionBar();
ab.setTitle(title);
Window=getWindow();
视图装饰=window.getDecorView();
ArrayList视图=新建ArrayList();
装饰。使用文本查找视图(视图、标题、视图。使用文本查找视图);
用于(视图:视图){
Log.d(标记“view”+view.toString());
}
TextView tvTitle=(TextView)decor.findViewById(views.get(0.getId());
tvTitle.setBackgroundColor(颜色:红色);
}
以下是如何从操作栏获取文本视图。此方法确保即使ActionBar方向更改,也会选择TextView。如果需要,它还允许您获取图标-只需检查ImageView的子实例
//If you just want to set the text
ActionBar actBar = getSupportActionBar();
if(actBar != null) {
actBar.setTitle(R.string.your_ab_title);
}
//If you want to customize more than the text
Toolbar ab = findViewById(R.id.action_bar);
if(ab != null){
for (int i= 0; i < ab.getChildCount(); i++){
if(ab.getChildAt(i) instanceof TextView) {
TextView title = (TextView) ab.getChildAt(i);
//You now have the title textView. Do something with it
title.setText("Your Custom AB Title");
}
}
}
//如果您只想设置文本
ActionBar actBar=getSupportActionBar();
如果(actBar!=null){
actibar.setTitle(R.string.your_ab_title);
}
//如果您想自定义更多的文本
工具栏ab=findViewById(R.id.action\u栏);
如果(ab!=null){
对于(inti=0;i
嘿,谢谢大家的帮助,这里是我最后的实现
// USAGE
Toolbar yourToolbar = (Toolbar) findViewById(R.id.your_toolbar);
TextView actionTitle = ActionBarTitle(toolbar);
actionTitle.setTypeface(font("Snake"));
// IMPLEMENTATION
private TextView ActionBarTitle(Toolbar toolbarForRead)
{
TextView title = null;
if (toolbarForRead != null)
{
for (int i= 0; i < toolbarForRead.getChildCount(); i++)
{
if (toolbarForRead.getChildAt(i) instanceof
TextView)
{
title = (TextView)
toolbarForRead.getChildAt(i);
return title;
}
}
}
return null;
}
private Typeface font(String fontName)
{
return Typeface.createFromAsset(this.getAssets(), "fonts/" + fontName + ".ttf");
}
//用法
Toolbar yourToolbar=(Toolbar)findviewbyd(R.id.your_Toolbar);
TextView actionTitle=ActionBarTitle(工具栏);
actionTitle.setTypeface(字体(“Snake”);
//实施
私有文本视图操作Bartitle(工具栏ForRead)
{
TextView title=null;
if(toolbarForRead!=null)
{
for(int i=0;i
我使用AppCompatActivity,这对我很有用
val titleId :Int = resources.getIdentifier("title","id",BuildConfig.APPLICATION_ID )
val title :TextView = findViewById(titleId)
你在使用工具栏吗?@r7v不,我不使用工具栏。@r7v工具栏和动作栏有什么区别吗?你的用例是什么?动作栏和工具栏有什么区别吗?你可以在Android开发者那里找到,需要对动作栏进行更多的控制,而工具栏就是为了它。换句话说,ActionBar现在变成了一种特殊的工具栏。您还应该看到,我使用的是
androidX
library,而mID=-1表示工具栏中包含标题的TextView
。
val titleId :Int = resources.getIdentifier("title","id",BuildConfig.APPLICATION_ID )
val title :TextView = findViewById(titleId)