Android之style样式文件的编写

在对Android空间的样式进行编写时,我们可能会对某一组样式用到非常多次,这样就会造成代码冗余,我们可以将这一组样式写入style文件中,然后通过引用的方式进行使用即可。具体操作流程如下:

首先创建在res/values文件夹下创建styles文件

Android之style样式文件的编写_第1张图片

 然后打开styles.xml进行代码编写


    
      
      
      
      
  1. "1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="text">
  4. <item name="android:textColor">@color/black item>
  5. <item name="android:textSize">16sp item>
  6. <item name="android:textStyle">bold item>
  7. style>
  8. resources>

然后在布局文件中引用


    
      
      
      
      
  1. "1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app= "http://schemas.android.com/apk/res-auto"
  4. xmlns:tools= "http://schemas.android.com/tools"
  5. android:layout_width= "match_parent"
  6. android:layout_height= "match_parent"
  7. tools:context= ".MainActivity"
  8. android:orientation= "vertical"
  9. >
  10. <TextView
  11. android:layout_width= "match_parent"
  12. android:layout_height= "100dp"
  13. android:gravity= "center"
  14. android:text= "测试1"
  15. style= "@style/text"
  16. />
  17. <TextView
  18. android:layout_width= "match_parent"
  19. android:layout_height= "100dp"
  20. android:gravity= "center"
  21. android:text= "测试2"
  22. android:textStyle= "bold"
  23. android:textSize= "16sp"
  24. android:textColor= "@color/black"
  25. />
  26. LinearLayout>

效果如下,可以发现效果时一样的

Android之style样式文件的编写_第2张图片

 

你可能感兴趣的:(android,android,android,studio,ide)