Creating a custom toolbar in android

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

enter image description here

The code I wrote for implementation looks 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> 

And the action 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 instead, I get the following: enter image description here

There are not many tutorials in customizing the advanced toolbar, so it’s very helpful to help.

+6
source share
2 answers

I think you just need to add gravity = "bottom" in the toolbar settings, for example:

  <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 needed to add some indentation at the bottom of the layout to get the edit, but it should get the text at the bottom of the edit.

Or you can set layout_gravity to 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"/> 

I am surprised that alignParentBottom compiles. I do not think the toolbar inherits from RelativeLayout.

Edit - Here is my full 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> 

The result is the following:

Toolbar

+2
source
 <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> 
0
source

Source: https://habr.com/ru/post/988182/


All Articles