我正在使用扩展高度 (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);
最佳答案
我相信工具栏的行为或多或少类似于 LinearLayout,即使它没有扩展它。
如果我是对的,您将无法按照您的 Intent 使用“两行”。
也许您可以将该 EditView 从工具栏中取出,或者使用 ActionBar
(带有“X”图标和操作)并在带有 EditText 的工具栏下方使用。
关于android - 如何正确地将自定义 View 添加到工具栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26573600/