如何正确添加自定义视图到工具栏?

时间:2014-10-26 13:40:14

标签: android toolbar

我正在使用扩展高度(56dp + 80dp)的工具栏,并希望将EditText添加到工具栏的底部。我遇到的问题是EditText不会将自身扩展到右边缘,如下图所示:

enter image description here

代码如下所示:

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

向工具栏添加布局:

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

2 个答案:

答案 0 :(得分:3)

我相信工具栏的行为或多或少类似于LinearLayout,即使它没有扩展它。

如果我是对的,你将无法使用&#34;两行&#34;就像你想要的那样。

也许您可以将EditView从工具栏中取出,或者使用ActionBar(使用&#39; X&#39;图标和操作)并在工具栏下方使用EditText。

答案 1 :(得分:-2)

您可以使用工具栏下方的EditText添加自定义布局,背景颜色相同。

相关问题