是否可以在xml中创建"dp"变量?

And*_*Kor 1 android

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

喜欢:

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

使用时width:

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

有什么办法吗?

Gia*_*ran 8

如下所示创建您的dp价值dimens.xml:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="width">16dp</dimen>
</resources>
Run Code Online (Sandbox Code Playgroud)

并使用

<ImageView
   android:layout_width="@dimen/width"
   android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)