1样式用在控件上
2样式是借鉴前端中的css思想
3在layout中的控件中引用
4样式的具体用法
layout----activity_main.xml中
Activity中(注意引用样式是activity直接继承Activity如果继承AppCompatActivity会报错)
2样式是借鉴前端中的css思想
3在layout中的控件中引用
4样式的具体用法
5注意引用样式是activity直接继承Activity如果继承AppCompatActivity会报错
效果图:
相关代码
values----styles.xml中
<span style="font-size:18px;"><resources>
<!--系统中定义的样式-->
<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>
<!-- 自己定义的样式,继承系统的样式AppTheme-->
<style name="mytext_style" parent="AppTheme">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FF0033</item>
<item name="android:textSize">35sp</item>
<item name="android:background">#00CCFF</item>
</style>
<!-- 继承自定义样式mytext_style-->
<!-- 继承的思想和java继承思想是一样的-->
<style name="mytext_style.mytext_style2">
</style>
</resources>
</span>
layout----activity_main.xml中
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<!-- 引用样式style="@style/mytext_style2"-->
<!-- 样式mytext_style2是继承mytext_style-->
<TextView
android:id="@+id/textview1"
android:text="我叫翟浩浩1"
style="@style/mytext_style.mytext_style2"
/>
<TextView
android:id="@+id/textview2"
android:text="我叫翟浩浩2"
style="@style/mytext_style.mytext_style2"
/>
<TextView
android:id="@+id/textview3"
android:text="我叫翟浩浩3"
style="@style/mytext_style.mytext_style2"
android:textColor="#ff00ff"
/>
</LinearLayout>
</span>
Activity中(注意引用样式是activity直接继承Activity如果继承AppCompatActivity会报错)
<span style="font-size:18px;">public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}</span>
源码下载