从strings.xml提供layout_height和layout_width

3
我正在尝试按照以下方式从strings.xml中的常量字符串设置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" />

strings.xml 中,我有:

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

但是,在执行setContentView(view_Id)时会出现错误。

错误为java.lang.RuntimeException: Binary XML file line #82: You must supply a layout_width attribute.

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

这有点奇怪,因为我使用同样的方法来设置EditTextmaxlength,它运行得很好。

我是否做错了什么?

任何帮助都将不胜感激。

7个回答

8

不要将"string"作为资源提供,而应该提供它作为"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文件中: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
您应该使用文件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" />

android:layout_width="@dimen" 或 android:layout_width="@dimen"? - Mohammad Afrashteh
@MohammadAfrashteh 当然是 dimen。错别字已经修正。谢谢。 - fasteque

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

android:layout_width="@dimen" 或 android:layout_width="@dimen"? - Mohammad Afrashteh

2
这是一种不正确的字符串资源使用方式。您应该使用Dimension来指定尺寸。

2
与其使用字符串资源,您应该使用尺寸资源。请参见此处:http://developer.android.com/guide/topics/resources/more-resources.html#Dimension
您可以在值文件夹中创建一个名为dimens.xml的文件(与strings.xml放置在同一位置)。
示例dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="continueBtnHeightHD">60dp</dimen>
  <dimen name="continueBtnWidthHD">250dp</dimen>
</resources>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接