2012-02-27 57 views
26

我需要为不同屏幕大小的设备设置不同的布局权重,并且我想从维度文件传递值,问题是维度文件中的引用不断抛出错误从维度(维度)传递layout_weight参考

error: Error: Integer types not allowed (at 'progress_widget_item3_weight' with value '9'). 

error: Error: Float types not allowed (at 'progress_widget_item3_weight' with value '9.1'). 

<dimen name="progress_widget_item3_weight">9</dimen> 

我如何通过从梦诗F的值ile为layout_weight?谢谢

回答

49

代替维数,您可以使用integer来声明和引用整数。

声明示例中integers.xml:在布局

<resources> 
    <integer name="left">1</integer> 
    <integer name="right">2</integer> 
</resources> 

实施例参考:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="@integer/left" /> 

    <FrameLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="@integer/right" /> 

</LinearLayout> 
+1

注意,使用@integer值layout_weight(其被指定为接受'float'值)会导致在某些设备(如LG Optimus L1/L3,Samsung Galaxy Y/Neo和Blackberry Q10)上发生'java.lang.NumberFormatException:无效浮动错误。 – Nachi 2015-05-20 14:02:41

3

我不认为你可以做到这一点。 xml文件中的文档说明只有float(不是dimension),而不是layout_width,例如您有dimension

您可以在不同的屏幕相关布局中使用硬编码的float值。