Android样式和主题

样式:

定义组件样式的时候有两种定义方法:

1.直接在main.xml的组件中定义样式

2.用style="@style/"引用values文件夹下的样式文件里定义的样式



main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        style="@style/style1"
        android:text="hahahaha1" />
    <TextView
        style="@style/style2"
        android:text="hahahaha2" />
    <TextView
        style="@style/style2.style1"
        android:text="hahahaha4" />
    
    <!-- 最基本的样式写法,直接写在组件里,和html标签中写style属性一个意思 -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hahahaha3"
        android:textSize="30sp"
        android:textColor="#0000FF" />
</LinearLayout>

string,xml(也可以新建一个样式的xml文件,在里边定义样式)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">TestStyle</string>
    <style name="style1">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#FF0000</item>
        <item name="android:textSize">50sp</item>
    </style>
    
    <!-- 样式的继承 -->
    <style name="style2" parent="style1">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#00FF00</item>
    </style>

    <!-- 样式的继承另一种写法 -->
    <style name="style2.style1">
    </style>
</resources>



主题:

主题就是清单文件中的android:theme属性,当这个属性在application组件中就是做用于所有Activity,当这个属性在某个Activity上,就表示对某个Activity有效。


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxc.style"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
	<!-- android:theme也可以定义在application-->
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <!-- android:theme用来定义Activity样式的 -->
        <activity
            android:theme="@android:style/Theme.Dialog"
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值