4
我在XML文件中定義的DP尺寸,像這樣:Android的XML定義尺寸值產生意想不到的效果
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="custom_button_Margin">10dp</dimen>
</resources>
的想法是,我使用這些值來設置元素之間的空白。這在我的佈局XML文件中使用該值時正常工作。
段:
<RelativeLayout
android:id="@+id/mainButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.4"
android:layout_margin = "5dp"
android:gravity="right|bottom">
<Button
android:id="@+id/_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/seven"
android:background = "@drawable/custom_button"
android:typeface="monospace"
android:textSize="@dimen/custom_button_TextSize"
android:layout_marginRight = "@dimen/custom_button_Margin"
android:layout_marginBottom = "@dimen/custom_button_Margin"
/>
</RelativeLayout>
當我嘗試以編程方式獲取此值的問題所在。我希望能得到一個與屏幕密度相匹配的值。那麼我會做的就是按照公式發現here(頁面很長找dp單位轉換爲像素爲單位)
我包檢索的文件中定義的值的函數公式,秤它像素。
private int get_custom_button_PadV()
{
final float scale = getResources().getDisplayMetrics().density;
return (int) (R.dimen.custom_button_Margin * scale + 0.5f);
}
當我看的代碼,我看到下面的值
scale = 1.0
R.dimen.custom_button_Margin = 2131099650
我想不通爲什麼custom_button_Margin值是如此之大......我希望,隨着1.0的比例,那會有一個值10.我錯過了什麼?
感謝您的解釋。這完全解決了這個問題。 – paradoX 2011-03-27 18:07:46