2014-10-26 107 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