通过其整数引用使用ScaleType枚举值

时间:2016-05-15 09:13:22

标签: java android enums scaletype

为了清楚地表达我的问题,我想在布局xml中使用ScaleType根据布局宽度使用不同的值(例如sw-600,sw-720,...等),因为它没有' t有一个明确的常量值,比如布局权重,例如我不知道是否可以实现这一点,所以我查看了ImageView class并发现ScaleType值为enum }使用赋值,是否可以使用枚举其赋值?所以当在xml中使用它时,它将是

android:scaleType="2" // 2 will be an Integer value assigned differently across different layouts

而不是:

android:scaleType="FIT_START"

2 个答案:

答案 0 :(得分:1)

这就是我所做的,到目前为止一切顺利。我有一个资源包含不同的值,具体取决于res文件夹类型。

w720dp \ sizes.xml

<integer name="advert_scaletype">3</integer>

值\ sizes.xml

<integer name="advert_scaletype">6</integer>

通过这样做,如果我在平板电脑或手机上,我会在图像上获得不同的比例类型。

我使用它的方式:

 <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/advert_image"
        android:layout_width="match_parent"
        android:layout_height="230dp"
        android:layout_centerVertical="true"
        android:scaleType="@integer/advert_scaletype"
        android:transitionName="photo"/>

到目前为止我还没有发现任何问题,所以不确定为什么这对其他人不起作用。

如果要使用的值为 float ,则可以按以下格式将其存储在dimens.xml中:

<item name="advert_scaletype" format="float" type="dimen">7.6</item>

然后它会像:

 <com.android.volley.toolbox.NetworkImageView 
      ...
      android:scaleType="@dimens/advert_scaletype" />

答案 1 :(得分:1)

I had the same problem before and I'm posting here what I think is the best approach so far:

values\style.xml

<style name="my_imageview_parent_style">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_weight">match_parent</item>
</style>

<style name="my_imageview_child_style" parent="my_imageview_parent_style">
    <item name="android:scaleType">centerInside</item>
</style>

values-XXX\style.xml

<style name="my_imageview_child_style">
    <item name="android:scaleType">fitXY</item>
</style>

values-YYY\style.xml

<style name="my_imageview_child_style">
    <item name="android:scaleType">fitXY</item>
</style>
相关问题