在android中创建自定义工具栏

时间:2015-05-29 16:00:39

标签: android android-layout android-toolbar

我正在尝试使用工具栏中的编辑文本在android中创建自定义扩展工具栏。我想要实现的布局看起来像这样

enter image description here

我编写的代码实现如下:

<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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="256dp"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"

    >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchbox"
        android:layout_alignParentBottom="true"
        android:text="Test"
        android:background="#ffffff"
        />

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

活动有以下代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
    }}

但我得到的是: enter image description here

没有很多关于自定义扩展工具栏的教程,所以非常感谢一些帮助。

2 个答案:

答案 0 :(得分:2)

我认为你只需要添加引力=&#34;底部&#34;在工具栏设置上,如:

   <android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:gravity="bottom"
    android:layout_height="256dp"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

我不得不在布局的底部添加一些边距以使编辑出现,但是应该将文本放到编辑的底部。

或者您可以在EditText上设置layout_gravity。

        <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_gravity="bottom"
        android:id="@+id/searchbox"
        android:text="Test"
        android:background="#ffffff"/>

我很惊讶alignParentBottom编译。我不相信Toolbar继承自RelativeLayout。

编辑 - 这是我的完整布局:

<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
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="264dp"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_gravity="bottom"
        android:id="@+id/searchbox"
        android:text="Test"
        android:background="#ffffff"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>

结果如下:

ToolBar

答案 1 :(得分:0)

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="?attr/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginRight="20dp"
        android:background="@android:color/white">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:background="@null"
            android:inputType="text"
            android:textSize="22sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:padding="12dp"
            android:src="@drawable/ic_search" />
    </RelativeLayout>
</android.support.v7.widget.Toolbar>
相关问题