Reputation: 476
My app's minSdkVersion
is < 17
. I want to change position and direction of a LinearLayout programmatically instead of defining two different LinearLayouts.
Old solution:
<LinearLayout
android:visibility="gone"
android:id="@+id/toolbar_vertical_outside"
...
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_alignTop="@id/aView"
android:layout_toEndOf="@id/aView"
android:layout_toRightOf="@id/aView">
...
<LinearLayout
android:visibility="gone"
android:id="@+id/toolbar_horizontal_inside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_alignEnd="@id/aView"
android:layout_alignRight="@id/aView"
android:layout_alignBottom="@id/aView">
...
New solution:
<LinearLayout
android:id="@+id/toolbar"
...
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="@style/Toolbar_VerticalOutside">
...
... including new styles:
<style name="Toolbar_VerticalOutside">
<item name="android:orientation">vertical</item>
<item name="android:layout_alignTop">@id/aView</item>
<item name="android:layout_toEndOf">@id/aView</item>
<item name="android:layout_toRightOf">@id/aView</item>
</style>
<style name="Toolbar_HorizontalInside">
<item name="android:orientation">horizontal</item>
<item name="android:layout_alignEnd">@id/aView</item>
<item name="android:layout_alignRight">@id/aView</item>
<item name="android:layout_alignBottom">@id/aView</item>
</style>
Problem: For the new solution, Android Studio complains about name="android:layout_toEndOf"
and name="android:layout_alignEnd"
: "... requires API level 17 (current min is 14)". But it didn't complain about the same properties when they were used in a layout file. Why is Android Studio more tolerant about new properties in a layout than in styles? I want to use new properties in styles, too.
Upvotes: 0
Views: 50
Reputation: 54204
This is just a quirk of how the Android Studio linter works.
In your layout, you've included
android:layout_alignEnd="@id/aView"
android:layout_alignRight="@id/aView"
And you get no warnings/errors. That is because you have both defined. If you delete just alignEnd
, the linter will warn that you should add alignEnd
to support right-to-left layouts. If you delete just alignRight
, the linter will warn that you should add alignRight
to support pre-17 API levels.
In styles, this "check if both are present" behavior isn't there. The linter just checks each <item>
separately, so it warns that alignEnd
requires API 17. However, this won't prevent your app from compiling/running.
If you really wanted to be a good citizen, you'd define two separate styles within two resource directories.
In /res/values/styles.xml
:
<style name="Toolbar_HorizontalInside">
<item name="android:orientation">horizontal</item>
<item name="android:layout_alignRight">@id/aView</item>
<item name="android:layout_alignBottom">@id/aView</item>
</style>
In /res/values-v17/styles.xml
:
<style name="Toolbar_HorizontalInside">
<item name="android:orientation">horizontal</item>
<item name="android:layout_alignEnd">@id/aView</item>
<item name="android:layout_alignBottom">@id/aView</item>
</style>
Upvotes: 1