问题
So I am comfortable with using relative layouts, but whilst getting used to Android Studio I noticed that in my relative layout child views it generated both of the following.
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true
I have checked out the Android docs here, but cannot see a distinction between the two. Certainly swapping one for another in the Android Studio shows no visible difference. Is there one?
回答1:
It depends on the layout direction. The layout direction can be either left-to-right (start = left, end = right), or right-to-left (vice versa).
By default, the layout direction is based on the locale (left-to-right for languages like English, right-to-left for languages like Arabic), but you can override it with the layoutDirection XML attribute or setLayoutDirection function. e.g.:
android:layoutDirection="ltr"
^ will make alignParentStart equivalent to alignParentLeft on all devices.
android:layoutDirection="rtl"
^ will make alignParentStart equivalent to alignParentRight on all devices. You can also set to "locale" to use the locale or "inherit" to inherit the layout direction from the parent view.
You need to add android:supportsRtl="true"
to your AndroidManifest.xml to support right-to-left layouts.
also related: android:textDirection
回答2:
android:layout_alignParentStart="true"
Aligns the start edge of this view to the start edge of its parent. This is the left edge for LTR (left to right) locales and the right one on RTL (right to left) locale languages like Arabic, Hebrew, Persian etc.
The reason Android Studio also adds
android:layout_alignParentLeft="true"
to your views is to support older platforms that came before 4.2.x Jelly Bean
. The Start/End attributes like layout_alignParentStart
are only available from API 17 onwards. The newer platforms fallback to Left/Right attributes only if the corresponding Start/End attributes are not found.
In case, your application supports legacy platforms using android:minSdkVersion
below level 17
you must always provide Left/Right attributes for your views. Otherwise the project won't compile with an error message like
To support older versions than API 17 (project specifies 7) you should also add android:layout_alignParentLeft="true"
Also note that your Android application needs to declare its support for RTL locales within your AndroidManifest.xml
as well.
<application
...
android:supportsRtl="true"
/>
回答3:
These "xxxStart", "xxxEnd" attribute is to support RTL(Right to Left) layout in some locales. Such as
android:paddingStart
android:paddingEnd
android:layout_marginStart
android:layout_marginEnd
...
You can see more here about it.
In normal(left to right) layout, "xxxStart" means "xxxLeft" and "xxxEnd" means "xxxRight".But in Right to Left layout, "xxxStart" means "xxxRight" and "xxxEnd" means "xxxLeft".
But RTL is only supported on sdk 17 and higher.
To support lower sdk, you can use "android:layout_marginStart" along with "android:layout_marginLeft". At the lower sdk devices, "android:layout_marginLeft" will be used.
来源:https://stackoverflow.com/questions/27934512/relative-layout-alignparentleft-vs-alignparentstart