Reputation: 526
Maybe a stupid question but in android how can I set aspect ratio in xml (dimens.xml in values folder) to set different aspect ratio depending on devices
I tried :
layout.xml
app:layout_constraintDimensionRatio="@dimen/common_image_aspect_ratio"
values/dimens.xml
<dimen name="common_image_aspect_ratio">3:2</dimen>
but it doesn't seem to work.
Any help would be appreciated, thx :)
Upvotes: 2
Views: 872
Reputation: 2140
If you want to define a resource for in the form 3:2
you should use a String:
// values.xml
<string name="common_image_aspect_ratio">3:2</string>
// layout.xml
app:layout_constraintDimensionRatio="@string/common_image_aspect_ratio"
Using a String also applies when setting a ratio programatically:
(view.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio = "3:2"
Upvotes: 0
Reputation: 1506
Try to write 1.5
instead of 3:2
. Generally, you should use decimal format, not fractional. Hope it will help.
Upvotes: 5