在Android中将尺寸存储在xml文件中 [英] Storing dimensions in xml file in Android

查看:28
本文介绍了在Android中将尺寸存储在xml文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 android 应用程序的所有视图中始终使用相同的尺寸(例如,20dp 的左边距).如果我使用 HTML,我会简单地使用 CSS 文件,但我不知道如何在 Android 上执行此操作.

I am trying to consistently use the same dimension in all the views in my android app (e.g., a left margin of 20dp). If I were using HTML, I would simply use a CSS file, but I am at a loss of how to do this on Android.

有没有办法可以将此值存储在 res/values 内的 xml 文件中,然后在布局中使用它?

Is there a way I can store this value in a xml file inside res/values and then use it in layouts?

例如,我想将它们存储在strings.xml中

e.g., I thought of storing them in strings.xml like

<string name="app_wide_left_padding">20dp</string>

然后在 layout.xml 中使用以下文本

and then using the following text in layout.xml

android:paddingLeft="@string/app_wide_left_padding"

但我不确定它会起作用.我在正确的轨道上吗?

but I am not sure it will work. Am I on the right track?

预先感谢您的回答.

推荐答案

创建一个名为 res/values/dimens.xml 的文件

Create a file called res/values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="icon_width">55dip</dimen>
    <dimen name="icon_height">55dip</dimen>
    <dimen name="photo_width">170dip</dimen>
    <dimen name="photo_height">155dip</dimen>
</resources>

然后在您的其他 xml 中引用它们:

Then reference them in your other xml:

android:padding="@dimen/icon_width">

这篇关于在Android中将尺寸存储在xml文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆