2014-10-26 82 views
18

我正在使用具有扩展高度(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

您是否曾经找到过解决方案? – DDukesterman 2015-02-02 19:05:16

+3

其实我在答案中就像descibed - 在工具栏下面添加了自定义布局。恐怕现在没有其他解决方案了。 – miqqo 2015-02-07 19:12:35

回答

3

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

如果我是对的,你将无法像你打算使用“两排”。

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

+0

你能提供样品吗? – 2015-07-22 20:33:08

-2

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

+2

不。工具栏有一个阴影,所以这看起来很丑。 – Martin 2015-03-16 15:10:28

+0

绝对不好的决定。 Android操作系统为您提供了多种方法,不要试图用榴弹炮狩猎松鼠 – 2015-12-30 09:49:22

相关问题