如何正确地将自定义视图添加到工具栏? [英] How to properly add custom view to the toolbar?

查看:104
本文介绍了如何正确地将自定义视图添加到工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用扩展高度(56dp + 80dp)的工具栏,并想将EditText添加到工具栏的底部.我的问题是EditText不会自行扩展到右侧,如下图所示:

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:

代码如下:

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

推荐答案

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

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.

也许您可以将EditView移出工具栏,或者使用 ActionBar (带有'X'图标和动作)以及带有EditText的工具栏下方.

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.

这篇关于如何正确地将自定义视图添加到工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆