Load dimension value from res/values/dimension.xml from source code

前端 未结 9 1756
情书的邮戳
情书的邮戳 2020-11-27 09:30

I\'d like to load the value as it is. I have two dimension.xml files, one in /res/values/dimension.xml and the other one in /res/values-sw360

9条回答
  •  萌比男神i
    2020-11-27 09:58

    If you just want to change the size font dynamically then you can:

    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.tutorial_cross_marginTop))
    

    As @achie's answer, you can get the dp from dimens.xml like this:

    val dpValue = (resources.getDimension(R.dimen.tutorial_cross_marginTop)/ resources.displayMetrics.density).toInt()
    

    or get sp like this

    val spValue = (resources.getDimension(R.dimen.font_size)/ resources.displayMetrics.scaledDensity).toInt()
    

    About Resources.java #{getDimension}

        /**
         * Retrieve a dimensional for a particular resource ID.  Unit 
         * conversions are based on the current {@link DisplayMetrics} associated
         * with the resources.
         * 
         * @param id The desired resource identifier, as generated by the aapt
         *           tool. This integer encodes the package, type, and resource
         *           entry. The value 0 is an invalid identifier.
         * 
         * @return Resource dimension value multiplied by the appropriate 
         * metric.
         * 
         * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
         *
         * @see #getDimensionPixelOffset
         * @see #getDimensionPixelSize
         */
    

    Resource dimension value multiplied by the appropriate

提交回复
热议问题