从字符串.xml提供layout_height和layout_width



我正在尝试从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: 二进制 XML 文件行 #82: 您必须提供 layout_width 属性。

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

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

我做错了什么吗?

任何帮助表示赞赏。

不是提供"字符串"作为资源,而是提供"dimen"作为:

在迪门斯.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" />

高度和宽度应按如下方式使用

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

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

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

您可以在任何视图中使用@dimen/mydimen,也可以在需要在布局文件中对dp进行硬编码。

不要把它们放在字符串中.xml,把它们作为维度放在dimens.xml

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

然后在布局中.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" />

创建大小.xml 在值文件夹中如下所示:

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

在您的布局内.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" />

你应该使用文件 dimens.xml而不是字符串.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" />
这是

对字符串资源的不正确使用。您应该使用维度来指定维度。

不应

为此使用字符串资源,而应使用维度资源。看这里:

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

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

示例 dimen.xml:

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

相关内容

最新更新