Java 从strings.xml中提供布局高度和布局宽度

Java 从strings.xml中提供布局高度和布局宽度,java,android,xml,android-layout,android-imageview,Java,Android,Xml,Android Layout,Android Imageview,我试图从strings.xml中的常量string设置ImageView的高度和宽度,如下所示: <ImageView android:id="@+id/GetMailing" android:layout_width="@string/continueBtnWidthHD" android:layout_height="@string/continueBtnHeightHD" android

我试图从
strings.xml
中的常量string设置
ImageView
的高度和宽度,如下所示:

 <ImageView
            android:id="@+id/GetMailing"
            android:layout_width="@string/continueBtnWidthHD"
            android:layout_height="@string/continueBtnHeightHD"
            android:layout_gravity="center"
            android:src="@drawable/continue_button_style" />
但是执行
setContentView(view\u Id)
时会出现错误

错误为java.lang.RuntimeException:二进制XML文件行#82:必须提供布局宽度属性。

我使用这种方法是因为我想在不同的地方使用相同的值

这有点奇怪,因为我使用相同的方法来设置
EditText
maxlength
,它工作正常

我做错什么了吗


感谢您的帮助。

这是对字符串资源的错误使用。您应该使用来指定维度。

不要将它们放在strings.xml中,请将它们作为维度放在
dimens.xml中:

<dimen name="continueBtnHeightHD">60dp</dimen>
<dimen name="continueBtnWidthHD">250dp</dimen>
60dp
250dp
然后在layout.xml中:

<ImageView
            android:id="@+id/GetMailing"
            android:layout_width="@dimens/continueBtnWidthHD"
            android:layout_height="@dimens/continueBtnHeightHD"
            android:layout_gravity="center"
            android:src="@drawable/continue_button_style" />

为此,您应该使用维度资源,而不是使用字符串资源。请看这里:

您可以在values文件夹(与放置strings.xml的位置相同)中创建一个文件dimens.xml

dimens.xml示例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="continueBtnHeightHD">60dp</dimen>
  <dimen name="continueBtnWidthHD">250dp</dimen>
</resources>

60dp
250dp

您应该使用文件dimens.xml,而不是strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="continueBtnHeightHD">60dp</dimen>
<dimen name="continueBtnWidthHD">250dp</dimen>
</resources>

60dp
250dp
然后,在布局中,以以下方式引用这些值:

<ImageView
            android:id="@+id/GetMailing"
            android:layout_width="@dimen/continueBtnWidthHD"
            android:layout_height="@dimen/continueBtnHeightHD"
            android:layout_gravity="center"
            android:src="@drawable/continue_button_style" />

高度和宽度的使用方法如下

<resources>
    <dimen name="my_dimen">10dip</dimen>
</resources>
您可以在任何视图中使用
@dimen/mydimen
,也可以在需要硬编码布局文件中的
dp
时使用

不提供“string”作为资源,而是提供“dimen”作为:

在dimens.xml中

<dimen name="continueBtnHeightHD">60dp</dimen>
<dimen name="continueBtnWidthHD">250dp</dimen>
60dp
250dp
在活动xml文件中

 <ImageView
            android:id="@+id/GetMailing"
            android:layout_width="@dimen/continueBtnWidthHD"
            android:layout_height="@dimen/continueBtnHeightHD"
            android:layout_gravity="center"
            android:src="@drawable/continue_button_style" />

在values文件夹中创建size.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="continueBtnHeightHD">60dp</dimen>
<dimen name="continueBtnWidthHD">250dp</dimen>
</resources>

60dp
250dp
在layout.xml中

<ImageView
            android:id="@+id/GetMailing"
            android:layout_width="@dimens/continueBtnWidthHD"
            android:layout_height="@dimens/continueBtnHeightHD"
            android:layout_gravity="center"
            android:src="@drawable/continue_button_style" />

android:layout_width=“@dimens或android:layout_width=“@dimen?android:layou width=“@dimens或android:layou width=“@dimen?MohammadAfrashteh?”当然是
dimen
。打字错误修正。谢谢
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="continueBtnHeightHD">60dp</dimen>
<dimen name="continueBtnWidthHD">250dp</dimen>
</resources>
<ImageView
            android:id="@+id/GetMailing"
            android:layout_width="@dimens/continueBtnWidthHD"
            android:layout_height="@dimens/continueBtnHeightHD"
            android:layout_gravity="center"
            android:src="@drawable/continue_button_style" />