标题等没有出现在Android的工具栏出现在、工具栏、标题、Android

2023-09-05 00:24:55 作者:有含义的

我刚刚加一个Android工具栏的应用程序,我工作。该工具栏的位置可以正常工作使用工具栏的布局出现在显示屏的顶部。但是,工具栏显示为空白颜色条......在App Title和溢图标没有在工具栏上显示出来。就如何解决任何想法?

ToolbarActivity.java:

 公共抽象类ToolbarActivity扩展ActionBarActivity {

私人工具条工具栏;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_home);
    工具栏=(栏)findViewById(R.id.toolbar);
    如果(工具栏!= NULL){
        setSupportActionBar(工具栏);
        getSupportActionBar()setHomeButtonEnabled(真)。
        getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
    toolbar.setTitle(应用程序名);
    }
}
 

toolbar.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< android.support.v7.widget.Toolbar
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID /工具栏
机器人:layout_height =WRAP_CONTENT
机器人:layout_width =match_parent
机器人:=了minHeight?ATTR / actionBarSize
机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark
机器人:背景=@色/ ColorPrimary>
< /android.support.v7.widget.Toolbar>

menu_main.xml:
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
工具:上下文=MainActivity。>
<项目机器人:ID =@ + ID / action_settings
    机器人:标题=@字符串/ action_settings
    机器人:orderInCategory =100
    应用程序:showAsAction =总是/>
< /菜单>
 

style.xml:

 <样式名称=AppTheme父=Theme.AppCompat.Light.NoActionBar>
      <项目名称=windowActionBar>假< /项目>
      <项目名称=机器人:windowNoTitle>真< /项目>
      <项目名称=colorPrimary> @色/ ColorPrimary< /项目>
      <项目名称=colorPrimaryDark> @色/ ColorPrimaryDark< /项目>
< /风格>
 
打开多个文档时,如果让文档标题同时显示在工具栏下方 如下图所示

解决方案

编辑:好了,所以这需要一个完全不同的工作方式进行设置。我认为(因为我用的时候没有工作与工具栏许多未知数,即 setActionBar(工具栏)它增加自定义工具栏自动将内容视图 - 但事实并非如此。

因此​​,最大的问题是,工具栏从不添加到当前内容视图,因此将永远不会看到,直到你真正添加工具栏的当前内容视图。

有不同的方法,但我只是要表明的它们。

而不是有一个 toolbar.xml 文件,您应该添加工具栏直接进入的布局文件活动

这里的活动类和XML与活动一起去的一个例子:

MainActivity.java

 公共类MainActivity扩展ActionBarActivity {
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        工具条工具栏=(栏)findViewById(R.id.toolbar);
        如果(工具栏!= NULL){
            setSupportActionBar(工具栏);
            getSupportActionBar()的setTitle(我的自定义工具栏!)。
            getSupportActionBar()setHomeButtonEnabled(真)。
            getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
        }
    }
}
 

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=MainActivity。>

    < android.support.v7.widget.Toolbar的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID /工具栏
        机器人:layout_width =match_parent
        机器人:layout_height =100dp
        机器人:背景=@机器人:彩色/ holo_red_light
        机器人:=了minHeight100dp>

    < /android.support.v7.widget.Toolbar>

< / RelativeLayout的>
 

styles.xml 文件是正确的,因为它是。

要注意,你的动作条设置为自定义工具栏后,您可以使用默认的方法,这一点很重要如何改变的动作条的样子。比如设置标题,应该从 getSupportActionBar()设置。的setTitle(),而不是从 toolbar.setTitle()

I've just added an Android Toolbar to an app I'm working on. The toolbar placement works correctly with the toolbar layout appearing at the top of the display. However, the toolbar is showing up as a blank colored bar...the App Title and Overflow Icon do not show up in the Toolbar. Any ideas on how to fix?

ToolbarActivity.java:

public abstract class ToolbarActivity extends ActionBarActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); 
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setTitle("app name");
    }
}

toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@color/ColorPrimary" >
</android.support.v7.widget.Toolbar>

menu_main.xml:
<menu 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"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="always" />
</menu>

style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="windowActionBar">false</item>
      <item name="android:windowNoTitle">true</item>
      <item name="colorPrimary">@color/ColorPrimary</item>
      <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
</style>

解决方案

EDIT: Ok so this needs to be set in a completely different way to work. I assumed (because I hadn't worked with the ToolBar much yet, that when using setActionBar(toolbar) it added the custom ToolBar to the content view automatically - but that wasn't the case.

So the big issue is that the ToolBar is never added to the current content view and hence will never be visible until you actually add the ToolBar to the current content view.

There are different approaches, but I'm only gonna show one of the them.

Instead of having a toolbar.xml file, you should add the ToolBar directly into the layout file of the Activity.

Here's an example of the Activity class and the XML to go together with the Activity:

MainActivity.java

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if(toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("My custom toolbar!");
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"
        android:minHeight="100dp">

    </android.support.v7.widget.Toolbar>

</RelativeLayout>

Your styles.xml file is correct as it is.

It's important to note, that after setting your ActionBar to the custom ToolBar, you use the default methods for changing how the ActionBar looks like. For instance setting the title, should be set from getSupportActionBar().setTitle() and not from toolbar.setTitle().