【问题标题】:Toolbar does not appear android工具栏不出现android
【发布时间】:2016-05-31 16:15:22
【问题描述】:

我是第一次尝试使用一些 Android 代码。我从一开始就一直关注 Hello World 示例,直到您创建一个文本输入,然后启动一个不同的 Activity 并通过按下发送按钮发送文本输入内容 Activity。

现在我想为导航添加一个底部工具栏,因此我实现了一个单独的空活动来定义工具栏,然后我在我的主要活动中。但是正如您所看到的,实际上没有工具栏出现,但是我的文本输入本身已经消失,我的发送按钮现在出现在其他地方......老实说,我真的不知道我在做什么,所以如果我能得到一些解释,我会高度欣赏它:

更新:我已经尝试了@prat 的解决方案,请参阅附加的新屏幕截图。

更新 2:通过将 relativelayout 设置为垂直而不是水平来解决。

我最终想要实现的是一个类似于 Google 材料设计指南底部更新的导航栏的栏:

【问题讨论】:

  • 您已将方向设置为水平。您是否将其设置为MyActivity.java 中的工具栏?
  • 我看不到它说它是水平的,这不是水平导航栏的意义吗?还有为什么是 MyActivity.java?
  • 您的 LinearLayout 设置为水平。这就是为什么它在右边而不是底部。这是工具栏的示例:stackoverflow.com/a/30063604/4583267

标签: android android-layout android-studio


【解决方案1】:

对于底部工具栏。试试这个代码。

<RelativeLayout 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_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:layout_alignParentBottom="true"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello_android" />
    </RelativeLayout>
</RelativeLayout>

【讨论】:

  • 我已经使用@prat 的解决方案更新了上面的帖子,似乎没有成功,你知道出了什么问题吗?
  • @FlorianMonfort 试试这个github链接github.com/DevLight-Mobile-Agency/NavigationTabBar
  • 我宁愿不使用预先构建的框架,而是自己学习如何做到这一点,即使它不是那么好
  • 想通了!我查看了差异,因此它不是在 ToolBar XML 部分中,而是在相对布局中,它必须设置为垂直而不是水平。没有查看正确的代码块。我想这就是你的意思@Exception Lover
  • 是的,对不起,就像我说的那样,我没有查看正确的代码块,也没有看到您的评论应用在哪里......我的错
【解决方案2】:

对于底部的工具栏,试试这样

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/primaryBlue"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

ma​​in.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="wrap_content"
    android:orientation="vertical"
    tools:context="com.ahotbrew.toolbar.MainActivity">

    <include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp">

        <TextView
            android:text="hello_brew"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
</RelativeLayout>

【讨论】:

  • 我正在尝试您的解决方案,但我的颜色属性似乎有问题:该行为红色且 AS 表示“无法解析符号 @color/primaryBlue”...?
  • 对于颜色,您必须在 color.xml 文件中提及颜色。你可以提到你自己的颜色,可以从xml调用
  • Arf 我无法在这里显示结果,但它并没有真正达到我的预期......
  • 我已经根据你的代码用新结果更新了上面的帖子,到目前为止似乎还没有成功
  • 删除具有工具栏的第二个 xml 中的相对布局。我检查了相同的代码 ..fr 我工作正常
猜你喜欢
  • 2014-01-25
  • 2015-12-23
  • 2015-04-23
  • 2015-01-29
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多