添加视图到工具栏

时间:2015-05-25 14:46:51

标签: android

我已经搜索过这个问题但尚未找到有用的答案。 我正在尝试创建一个带有某种edittext的工具栏。 它应该是这样的:

enter image description here

我的XML文件应该如何?目前它看起来像这样:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/activity_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="8dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/activity_main_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/title"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:textSize="22sp"/>

        </LinearLayout>

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

活动如下:

private Toolbar toolbar;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.acitivty_main);

  toolbar = (Toolbar)findViewbyid(R.id.activity_main_toolbar);

  setSupportActionbar(toolbar);

}

但结果是一些Edittext位于工具栏的中心,没有工具栏标题的空间(请忽略“保存”菜单按钮)

enter image description here

现在我的问题是,

  1. 如何正确地将一个或多个视图添加到工具栏下方主操作栏高度?
  2. 你能推荐一些示例或教程页吗?
  3. 使用浮动提示在显示的图像中使用了哪种EditText?
  4. 提前致谢

1 个答案:

答案 0 :(得分:0)

首先,我认为首先回答第二个和第三个问题很容易,您可以在“Android设计支持库”的“Android开发者博客”中找到该信息。这里链接:

http://android-developers.blogspot.co.uk/2015_05_01_archive.html

向下滚动,直至找到名为“浮动标签以编辑文字”的部分并进行排序。

现在对于第一个问题,我做的是我制作了一个“工具栏”布局(此时我不能在这里做太多详细说明),然后将其添加到布局中。代码如下:

工具栏布局:

<?xml version="1.0" encoding="utf-8"?>
<!-- NOTE: Use To Maintain Structure Of Actionbar -->
<Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:background="#009688"
    android:id="@+id/toolBar"
    android:elevation="2dp">
    <!-- NOTE: Required For Top Section Structure -->
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="vertical">
        <EditText
            android:background="@android:color/transparent"
            android:hint="@string/note_title"
            android:textColorHint="#FFFFFF"
            android:id="@+id/actionTitle"
            android:paddingRight="16dp"
            style="@style/ActionText"/>
        <TextView
            android:id="@+id/textDate"
            style="@style/ActionDate"/>
    </LinearLayout>
</Toolbar>

将其添加到布局文件

<?xml version="1.0" encoding="utf-8"?>
<!-- NOTE: Layout For Edit Note Activity -->
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">
    <!-- NOTE: Assigned Custom Toolbar -->
    <include
        android:id="@+id/toolBar"
        layout="@layout/tool"/>
</RelativeLayout>

这应该可以解决你的问题(然后你可以搞乱使用动作栏属性来清理它:getActionBar()。setElevation(0);以及 getActionBar()的setTitle( “”);

相关问题