Android中style的使用2,样式的继承(借鉴css思想)

1样式用在控件上
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>



源码下载

http://download.csdn.net/detail/zhaihaohao1/9473253

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值