将EditText添加到工具栏

时间:2015-05-01 01:53:10

标签: android android-edittext android-custom-view toolbar

我的ToolbarEditText如下所示:

enter image description here

我尝试将其添加为ActionView,但我得到了这个结果:

enter image description here

它基本上只是添加了我的单词' Tap Below'作为没有TextView

的标题

这就是我的所作所为:

menu.xml文件

<menu>
    ... 
    ...
    <item
        android:id="@+id/edit_text_menu"
        android:orderInCategory="300"
        android:title="Tap Below"
        android:actionLayout="@layout/edit_text_layout"
        app:showAsAction="ifRoom|collapseActionView"
        />
</menu>

edit_text_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myEditText"
    android:background="#000000">

</EditText>

我想通过这样做,我将EditText布局添加到Toolbar ... 有人可以帮忙吗?

或者,我可以在EditText上覆盖Toolbar吗?

我尝试添加<item name="windowActionModeOverlay">true</item>,但它没有做任何事情。

3 个答案:

答案 0 :(得分:32)

只需将EditText添加到ToolBar的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.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
      <EditText
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/myEditText"
         android:background="#000000" />
</android.support.v7.widget.Toolbar>

答案 1 :(得分:6)

  1. 将EditText添加到工具栏xml。

  2. Toolbar添加到顶部的Activity's xml布局中。

  3. 将工具栏设置为ActionBar:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    
  4. 这将使工具栏成为&#34;内置ActionBar的{​​{1}}。

答案 2 :(得分:0)

使用belove代码

       <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:titleTextColor="@color/colorAccent"
        app:theme="@style/ToolbarColoredBackArrow"
        app:subtitleTextColor="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:textColor="@color/colorAccent"
            android:text="June 14"
            android:textAllCaps="true"
            android:layout_gravity="right"
            android:layout_marginRight="11dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_daily" />

    </android.support.v7.widget.Toolbar>
相关问题