首页 > 解决方案 > 是否可以在 xml 中创建“dp”变量?

问题描述

我想使用变量指定 adrawable的宽度/高度dp

像:

<dp name="width">30</dp>

使用时width

android:layout_width=width就像说android:layout_width="30dp"

有什么办法可以做到这一点?

标签: android

解决方案


像下面这样创造你的dp价值:dimens.xml

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="width">16dp</dimen>
</resources>

并使用

<ImageView
   android:layout_width="@dimen/width"
   android:layout_height="match_parent" />

推荐阅读