安卓开发设置文本字体大小以及设置字体颜色,显示在文本中间
时间: 2023-12-10 11:05:58 浏览: 137
设置文本字体大小:
在布局文件中,使用TextView来显示文本。可以通过android:textSize属性来设置字体大小,如下所示:
```xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Hello World!" />
```
其中,android:textSize属性的值可以是sp、dp或px,建议使用sp。
设置字体颜色:
同样在布局文件中,可以使用android:textColor属性来设置字体颜色。如下所示:
```xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:text="Hello World!" />
```
其中,android:textColor属性的值可以是颜色代码、颜色资源等。
显示文本在中间:
可以通过设置android:gravity属性来使文本显示在中间,如下所示:
```xml
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:gravity="center" />
```
其中,android:gravity属性的值可以是center、center_horizontal、center_vertical等。
阅读全文
相关推荐















