【问题标题】:How to properly add custom view to the toolbar?如何正确地将自定义视图添加到工具栏?
【发布时间】:2014-10-26 13:40:14
【问题描述】:

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

代码如下:

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

【问题讨论】:

  • 你找到解决方案了吗?
  • 实际上我在答案中做了描述 - 确实在工具栏下方添加了自定义布局。恐怕目前没有其他解决方案。

标签: android toolbar


【解决方案1】:

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

如果我是对的,您将无法按照您的意图使用“两行”。

也许您可以将该 EditView 从工具栏中取出,或者使用ActionBar(带有“X”图标和操作)并在带有 EditText 的工具栏下方使用。

【讨论】:

  • 可以提供样品吗?
【解决方案2】:

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

【讨论】:

  • 没有。工具栏有一个阴影,所以看起来很难看。
  • 绝对错误的决定。 Android OS为你提供了多种方法,不要试图用榴弹炮打松鼠
猜你喜欢
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多