On Android 6.0 Marshmallow the positioning of an EditText in relation to an ImageView in a RelativeLayout with the attributes baseline and layout_alignBaseline does not work anymore. The behaviour can even be observed in the layout editor in Android studio. On a real Android Marshmallow device or emulatr the EditText in my case is shift outside the screen and not visible. Do I miss some changes, is it a bug?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/imageView"
android:src="@drawable/icon_category_geography_raster"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:baselineAlignBottom="true"
android:background="#00ffff"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/imageView"
android:background="#ff0000"
android:inputType="text"
android:hint="Hint"
android:layout_alignBaseline="@id/imageView" />
I have created screenshots of the Layout Editor of Android Studio. The difference between these two screenshots is solely the change of the API level.
I have created a sample project https://github.com/mikegr/baseline
This is bug reported on https://code.google.com/p/android/issues/detail?id=73697&thanks=73697
Since API11 you can simply use android:baselineAlignBottom="true"
Below API11 you can overwrite getBaseLine()
.
Update to Android Support Library 23.2.1, this may solve your problem.
来源:https://stackoverflow.com/questions/32991273/marshmallow-relativelayout-behaves-strange-with-alignbaseline