简体   繁体   English

如何在程序内将布局和文本大小设置为 DP?

[英]How do I set Layout and Text size to DP inside program?

Basically I have this inside XML, but I have to recreate it inside a code.基本上我在 XML 中有这个,但我必须在代码中重新创建它。 How do I do it?我该怎么做?

<EditText 
        android:layout_width="140dp"
        android:layout_height="20dp"
        android:background="@drawable/input_bg01"
        android:textSize="10dp"
        android:gravity="center"
        android:text="111-222-333     FOOO" />

I can use this to set text size, but what about the layout_width and height?我可以用它来设置文本大小,但是 layout_width 和 height 呢?

edTxt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);

Is there a way to tell the code to use DP unit instead of pixel?有没有办法告诉代码使用 DP 单位而不是像素? Or a conversion function to convert DP into pixel?或者是将DP转换成像素的转换函数?

You can use:您可以使用:

float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());

Now the value of pixels is equivalent to 10dp at the device's current screen density.现在, pixels值相当于设备当前屏幕密度下的10dp

The TypedValue contains other similar methods that help in conversion. TypedValue包含有助于转换的其他类似方法。

Solved here . 在这里解决

Extract:提炼:

DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
float dp = 20f;
float fpixels = metrics.density * dp;
int pixels = (int) (fpixels + 0.5f);

Just for completeness: Another solution (which I'd prefer) for the question is given here只是为了完整性:这里给出了该问题的另一个解决方案(我更喜欢)

setTextSize(float) expects a scaled pixel value. setTextSize(float)需要缩放的像素值。 So, setTextSize(10) would give you the desired result.所以, setTextSize(10)会给你想要的结果。 However, getDimension() and getDimensionPixelSize() return the size in units of pixels.但是, getDimension()getDimensionPixelSize()以像素为单位返回大小。

So for your example it would be所以对于你的例子,它会是

setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.edTxt_text_size));

where <dimen name="edtxt_text_size">10dp</dimen> is set in your dimens.xml file for example.例如,其中<dimen name="edtxt_text_size">10dp</dimen>在您的 dimens.xml 文件中设置。

Regarding setting the TEXT SIZE:关于设置文本大小:

For 2021 ...对于 2021 年...

Fortunately, the correct modern answer is just幸运的是,正确的现代答案是

textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 25);

or you can use COMPLEX_UNIT_SP, etc.或者您可以使用 COMPLEX_UNIT_SP 等。

Exactly matches xml for text size:与文本大小的 xml 完全匹配:

Checked, on all devices this produces exactly the same result as just setting it in xml:检查,在所有设备上,这产生的结果与仅在 xml 中设置它的结果完全相同:

在此处输入图片说明


Regarding setting, say, a layout parameter:关于设置,比如说,一个布局参数:

int h = 66; // the new value
ViewGroup.LayoutParams p = some_frame.getLayoutParams();
float fdp = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    h,
    getResources().getDisplayMetrics());
p.height = Math.round(fdp);
some_frame.setLayoutParams(p);

This appears to work correctly, on a test device.这似乎在测试设备上正常工作。 The value "h" is now the "same" as simply in the xml, enter 66dp.值“h”现在与 xml 中的“相同”,输入 66dp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM