从strings.xml提供layout_height和layout_width

问题描述 投票:2回答:7

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

 <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" />

strings.xml,我有:

<string name="continueBtnHeightHD">60dp</string>
<string name="continueBtnWidthHD">250dp</string>

但这会在执行setContentView(view_Id)时出错。

错误是java.lang.RuntimeException:二进制XML文件行#82:您必须提供layout_width属性。

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

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

我做错了吗?

任何帮助赞赏。

java android xml android-layout android-imageview
7个回答
7
投票

而不是提供“字符串”作为资源,你提供它“dimen”作为:

在dimens.xml中

<dimen name="continueBtnHeightHD">60dp</dimen>
<dimen name="continueBtnWidthHD">250dp</dimen>

在活动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" />

4
投票

高度和宽度意味着如下使用

<resources>
    <dimen name="my_dimen">10dip</dimen>
</resources>

现在,像这样使用布局文件中的上述属性

 android:layout_width="@dimens/my_dimen"
 android:layout_height="@dimens/my_dimen"

你可以在任何视图中使用@dimen/mydimen,并且只要你需要在布局文件中硬编码dp


3
投票

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

<dimen name="continueBtnHeightHD">60dp</dimen>
<dimen name="continueBtnWidthHD">250dp</dimen>

然后在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" />

3
投票

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

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

在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" />

3
投票

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

<?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="@dimen/continueBtnWidthHD"
            android:layout_height="@dimen/continueBtnHeightHD"
            android:layout_gravity="center"
            android:src="@drawable/continue_button_style" />

2
投票

这是对字符串资源的错误使用。您应该使用Dimension指定尺寸。


2
投票

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

http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

您可以在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>
© www.soinside.com 2019 - 2024. All rights reserved.