在对Android空间的样式进行编写时,我们可能会对某一组样式用到非常多次,这样就会造成代码冗余,我们可以将这一组样式写入style文件中,然后通过引用的方式进行使用即可。具体操作流程如下:
首先创建在res/values文件夹下创建styles文件
然后打开styles.xml进行代码编写
-
"1.0" encoding="utf-8"?>
-
<resources>
-
-
<style name="text">
-
-
-
<item name="android:textColor">@color/black
item>
-
-
-
<item name="android:textSize">16sp
item>
-
-
<item name="android:textStyle">bold
item>
-
style>
-
-
resources>
然后在布局文件中引用
-
"1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:app=
"http://schemas.android.com/apk/res-auto"
-
xmlns:tools=
"http://schemas.android.com/tools"
-
android:layout_width=
"match_parent"
-
android:layout_height=
"match_parent"
-
tools:context=
".MainActivity"
-
android:orientation=
"vertical"
-
>
-
-
-
<TextView
-
android:layout_width=
"match_parent"
-
android:layout_height=
"100dp"
-
android:gravity=
"center"
-
android:text=
"测试1"
-
style=
"@style/text"
-
/>
-
-
<TextView
-
android:layout_width=
"match_parent"
-
android:layout_height=
"100dp"
-
android:gravity=
"center"
-
android:text=
"测试2"
-
android:textStyle=
"bold"
-
android:textSize=
"16sp"
-
android:textColor=
"@color/black"
-
/>
-
-
LinearLayout>
效果如下,可以发现效果时一样的