以编程方式声明视图时应用了哪些单位?

在 axml 文件中添加视图时,可以简单地指定视图属性的大小和单位,例如:

<TextView
    android:TextSize = "10sp"
    android:layout_marginTop = "10dp" />

正如这个答案中所说,有特定目的的特定单位。

我的主要问题是,当以动态方式以编程方式(通过代码)应用大小时,适用于大小的单位是什么?

例如,当像这样声明 TextSize 时:

TextView tv = new TextView();tv.TextSize = 10;

文本大小的单位是什么?sp? DP?像素?

最重要的是,我怎样才能改变它们以满足我的需要?


翻过高山走不出你
浏览 150回答 2
2回答

墨色风雨

如果你以编程方式生成文本视图如下代码TextView tv = new TextView();tv.setTextSize(10); // Sets text in sp (Scaled Pixel).如果你想用其他单位设置文本大小,你可以通过以下方式实现。TextView tv = new TextView();tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 10); // Sets text in px (Pixel).tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10); // Sets text in dip (Device Independent Pixels).tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); // Sets text in sp (Scaled Pixel).tv.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10); // Sets text in pt (Points).tv.setTextSize(TypedValue.COMPLEX_UNIT_IN, 10); // Sets text in in (inches).tv.setTextSize(TypedValue.COMPLEX_UNIT_MM, 10); // Sets text in mm (millimeters).默认情况下,Android 使用“sp”作为文本大小,使用“px”作为视图大小。对于其他视图尺寸,我们可以设置为 px(像素),但如果您想自定义单位,您可以使用自定义方法/**&nbsp; &nbsp; &nbsp;* Converts dip to px.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param context -&nbsp; Context of calling class.&nbsp; &nbsp; &nbsp;* @param dip&nbsp; &nbsp; &nbsp;- Value in dip to convert.&nbsp; &nbsp; &nbsp;* @return - Converted px value.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static int convertDipToPixels(Context context, int dip) {&nbsp; &nbsp; &nbsp; &nbsp; if (context == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; Resources resources = context.getResources();&nbsp; &nbsp; &nbsp; &nbsp; float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, resources.getDisplayMetrics());&nbsp; &nbsp; &nbsp; &nbsp; return (int) px;&nbsp; &nbsp; }通过上述方法,您可以将 YOUR_DESIRED_UNIT 转换为像素,然后设置为查看。你可以更换TypedValue.COMPLEX_UNIT_DIP根据您的用例使用上述单位。您也可以反之亦然,让 px 下降,但我们不能分配给自定义单位来查看,所以这就是我这样使用它的原因。我希望我解释得很好。

慕无忌1623718

第一的:我认为您应该尽可能避免以编程方式设置大小。第二:px Pixels :对应于屏幕上的实际像素。dp 或 dip 与密度无关的像素-:基于屏幕物理密度的抽象单位。这些单位是相对于 160 dpi 屏幕的,因此 1 dp 是 160 dpi 屏幕上的一个像素sp Scale-independent Pixels- :这类似于 dp 单位,但它也根据用户的字体大小偏好进行缩放在你的第三个问题中,我认为:例如 :对于edittext,您不应该像这样对宽度使用常量:&nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="100dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_centerVertical="true"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/banklist_firstselectbank"&nbsp; &nbsp; &nbsp; &nbsp; style="@style/TextAppearanceHeadline2"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"/>我认为最好像这样使用边距开始和边距结束:&nbsp;<TextView&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_centerVertical="true"&nbsp; &nbsp; &nbsp; &nbsp; android:text="@string/banklist_firstselectbank"&nbsp; &nbsp; &nbsp; &nbsp; style="@style/TextAppearanceHeadline2"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginEnd="50dp"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_marginStart="50dp"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; />并尽可能多地使用:重力等字段而不是常数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java