在创建Android的自定义工具栏

在创建Android的自定义工具栏

本文介绍了在创建Android的自定义工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a custom extended toolbar in android with an edit text in the toolbar. The layout that I want to implement looks something like this

这是我写的要实现的code是这样的:

The code that I have written to implement is something like this:

<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>

和活动具有以下code

And the Activity has the following code

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);
    }}

但我得到的却是这样的:

But what I get instead is this:

有没有关于自定义扩展工具栏等等倒很AP preciate一些帮助很多教程。

There are not a lot of tutorials about customizing the extended toolbar so would really appreciate some help.

推荐答案

我觉得你只需要在工具栏上设置添加比重=底部,如:

I think you just need to add gravity="bottom" on the Toolbar settings like:

   <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">

我不得不一些余量添加到布局的底部,以获得显示在编辑但应该得到的文本编辑的底部。

I had to add some margin to the bottom of the layout to get the edit to appear but that should get the Text to the bottom of the edit.

或者你也可以在设定的EditText layout_gravity。

Or you can set layout_gravity on the EditText.

        <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编译。我不相信从工具栏继承的RelativeLayout

I'm surprised alignParentBottom compiles. I don't believe Toolbar inherits from RelativeLayout.

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

Edit - Here's my complete layout:

<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>

这会导致这样的:

Which results in this:

这篇关于在创建Android的自定义工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:22