How to properly add custom view to the toolbar?

六月ゝ 毕业季﹏ 提交于 2019-12-03 15:22:37

问题


I am using toolbar with extended height (56dp + 80dp) and want to add EditText to the bottom of the toolbar. The problem I have is that EditText DOES NOT expands itself to the right edge, like in picture below:

The code looks like below:

toolbar_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Title"
    android:singleLine="true" />

Adding layout to toolbar:

LayoutInflater inflater = LayoutInflater.from(mActivity.getActionBarToolbar().getContext());
    mToolbarLayout = (EditText) inflater.inflate(R.layout.toolbar_edit_text, null);
    Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.BOTTOM;
    mActivity.getActionBarToolbar().addView(mToolbarLayout, layoutParams);

回答1:


I believe the toolbar behaves more or less like a LinearLayout, even when it doesn't extend it.

If I'm right, you wouldn't be able to use "two rows" like you intend.

Maybe you can take that EditView out of the toolbar, or alternatively, use ActionBar (With the 'X' icon and the actions) and below a toolbar with the EditText.




回答2:


You can add a custom layout with EditText below the Toolbar, with the same background color.



来源:https://stackoverflow.com/questions/26573600/how-to-properly-add-custom-view-to-the-toolbar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!