#Android studio中的style ##定义style 首先我们应该知道,style是xml文件,也就是说,这种标签语言是非常好定义的,以TextView的使用简单示例,
<style name="TextView">
<item name="android:layout_width">1dp</item>
<item name="android:gravity">center</item>
<item name="android:padding">4dp</item>
<item name="android:textSize">14dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/Black</item>
</style>`
十分简单,那么他的位置应该在哪里呢?在Android studio切换成Android这个视图,
![在这里插入图片描述](https://img-blog.csdnimg.cn/9835c602c0344564b91caad84ca4523f.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQ2MjcyNzQ5,size_16,color_FFFFFF,t_70)
当然整个project视图也可以(个人习惯),在这个里面有一个res 进去找到values或者style,个人的文件设置习惯,然后里面会有styles.xml文件,或者是themes,里面有theme.xml文件,总之问题不大,就是这两个,一般,那如何判断呢,最简单的就是看,里面的标签有resources标签,然后再有style标签,有这两个就直接进去写就可以了。 给个例子
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="textStyle_01"> <!--字体一 样式-->
<item name="android:textSize"> 28dp</item> <!--设置大小-->
<item name="android:color">#FFFFFFFF</item> <!--设置颜色-->
</style>
</resources>
和上面那个例子结合来看 ##使用style
<?xml version="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"
android:orientation="vertical"
tools:context="com.example.lum.textstyle.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="15dp"
android:textColor="@color/colorPrimaryDark"/>
<TextView
style="@style/textStyle_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
注意,这里面有一个小盲点,这里面这个@style这个直接用,也就是说,无论你的style定义在style.xml中还是放在theme.xml中问题都不大,直接@style之后会给提示,而不是使用放在文件中的位置(即无需要@themes这种,直接用就可以了)
|