I'm using an EditText into the ActionBar as custom view.

LayoutInflater adbInflater = LayoutInflater.from(getActivity());
View etSearchView = adbInflater.inflate(R.layout.edit_text_search, null);
etSearch = (EditText) etSearchView.findViewById(R.id.search_edit_text);

//other etSearch properties are set

MainActivity.ab.setCustomView(etSearch);

edit_text_search.xml:

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text|textAutoCorrect"
    android:hint="@string/keywords"
    android:imeOptions="actionSearch"
    android:hapticFeedbackEnabled="true"
    android:singleLine="true"
    android:textCursorDrawable="@drawable/cursor"
    android:textColor="@android:color/white">
</EditText>

I have this behaviour... (undesired)

wrong

On the other hand, when the EditText's width and height are declared in code with

LayoutParams layoutParams = new LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
etSearch.setLayoutParams(layoutParams);

and removing android:layout_width="match_parent" and android:layout_height="wrap_content" from the xml file, I have this (desired) look...

right

How is this possible, with the same width and height?

Is this because, in code, they imply the addition of a wrapping layout?

有帮助吗?

解决方案

How is this possible, with the same width and height?

The layout params used in your EditText are different layout params than the ones applied when you add a View to the ActionBar. Every ViewGroup has a nested class that implements their own ViewGroup.LayoutParams.

By default, the ActionBar.LayoutParams are set to WRAP_CONTENT and MATCH_PARENT, so when you don't apply your own through ActionBar.setCustomView(View, LayoutParams), the ActionBar falls back on the default.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top