【问题标题】:Provide layout_height and layout_width from strings.xml从 strings.xml 提供 layout_height 和 layout_width
【发布时间】:2013-10-07 08:22:55
【问题描述】:

我正在尝试从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 属性。

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

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

我做错了什么吗?

任何帮助表示赞赏。

【问题讨论】:

    标签: java android xml android-layout android-imageview


    【解决方案1】:

    您不提供“字符串”作为资源,而是将其提供为“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" />
    

    【讨论】:

      【解决方案2】:

      Height 和 Width 的用法如下所示

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

        【讨论】:

          【解决方案4】:

          在 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="@dimens 或 android:layout_width="@dimen ?
          【解决方案5】:

          您应该使用文件 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="@dimens 或 android:layout_width="@dimen ?
          • @MohammadAfrashteh 当然是dimen。错字已修复。谢谢。
          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

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

            您可以在 values 文件夹中创建一个文件 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>
            

            【讨论】:

              猜你喜欢
              • 2015-04-07
              • 2016-05-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-02-17
              • 2012-10-30
              • 2013-06-09
              相关资源
              最近更新 更多