当前位置:首页 » 安卓系统 » androidstyle

androidstyle

发布时间: 2023-07-25 09:28:37

⑴ Android中style和theme的区别

Theme 和Style资源,将一些通用的属性提取出来,方便使用。

相同:
Theme和Style位于values文件夹下styles.xml下,格式相同。

[java] view plain
<style name="MyTransparent">
<item name="android:windowBackground">@color/transparent_background</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
</style>
<item name="Android:windowNoTitle">?android:windowIsTranslucent</item>其中 问号表示引用本主题中已经定义过的资源的值。

在需要使用的时候调用

[java] view plain
<EditText
android:id="@+id/nameEdit"
<span style="color:#ff0000;"> style="@style/LoginOrRegisterEditStyle"</span>
android:hint="@string/input_real_name"
android:layout_below="@id/tipHead"
android:layout_marginTop="20dp" />

不同:
Theme在AndroidManifest.xml中德Application节点或者Activity节点设置android:theme,或者在对应Activity中通过代码设置setTheme(),影响整个应用或者一个Activity.
Style一般是在布局中的View中设置。影响单个View,如EditText,TextView等等。

如果主题和样式属性有冲突,样式的属性高于主题。

如果需要使用系统自带的主题

[java] view plain
android:theme=<span style="color:#ff0000;">"@android:style</span>/Theme.Light.NoTitleBar"

⑵ android 可以在程序代码中设置style吗

Style是View中一些属性的集合,包括height,padding,fontcolor,background等等,Style单独定义在xml文件中,类似与web页面中css的角色,将设计和内容分开,便于修改和重复使用。

1 定义Style

style文件需要保存在res/values目录下,文件名任意,但是必须是xml文件,sytle文件的根标记必须是<resources>。写了一个简单示例,效果如下:

请参考android学习手册,例子、源码、文档全部搞定,采用androidstudo的目录结构,360手机助手中下载。下面是截图。

main.xml文件代码:

<?xmlversion="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">

<TextView

style="@style/CodeFont"

android:text="测试style">

</TextView>

</LinearLayout>

声明style是CodeFont,对应的是style文件中的style name。mystyle.xml文件中定义了style name是CodeFont:

parent属性表示style之间可以继承,同时可以覆盖parent style的一些属性。

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

<style name="CodeFont"parent="@android:style/TextAppearance.Medium">

<itemname="android:layout_width">fill_parent</item>

<itemname="android:layout_height">wrap_content</item>

<itemname="android:textColor">#00FF00</item>

<itemname="android:typeface">monospace</item>

</style>

</resources>

2 Style的继承

style继承有两种方式:

style的继承可以通过parent属性,用来继承android已经定义好的style,例如:

<style name="GreenText" parent="@android:style/TextAppearance">

<itemname="android:textColor">#00FF00</item>

</style>

继承了android中的TextAppearance,同时覆盖了android:textColor属性。

如果要继承自定义的style,不需要通过parent属性,只要style的name以需要继承的style的name开始后跟新的style的name,中间用“.”隔开。注意:这种方式只适用与自定义的style继承。

<style name="CodeFont.Red">

<itemname="android:textColor">#FF0000</item>

</style>

新的style继承了CodeFont,则在修改上边例子中的main.xml为:

<?xmlversion="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">

<TextView

style="@style/CodeFont.Red"

android:text="测试style">

</TextView>

</LinearLayout>


style也可以多级继承:

<stylename="CodeFont.Red.Big">

<itemname="android:textSize">30sp</item>

</style> sytle的更多属性见android包下的R.attr。需要注意,并不是所有的View都支持定义的style的属性,如果自定义的sytle中包含View不支持的属性,程序会自动忽略它。

⑶ android 怎么动态更改view 的style

Android 是可以使用 style的,具体方法为:
1、在Android中可以这样定义样式:
在res/values/styles.xml文件中添加以下内容
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name=“itcast”>
<item name="android:textSize">18px</item>
<item name="android:textColor">#0000CC</item>
</style>
<纤高/resources>
2、在layout文件中可以像下面这样使用上面的android样式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ....>
<TextView style="@style/itcast"
..... />
3、团竖带可以使他继承父样式,当然,如果父样式的值不符合需求,你也可以对它进行修改,如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="塌芦itcast">
<item name="android:textSize">18px</item>
<item name="android:textColor">#0000CC</item>
</style>
<style name="subitcast" parent="@style/itcast">
<item name="android:textColor">#FF0000</item>
</style>
</resources>

⑷ android中怎么创建style

自定义样式方法,可以直接通过定义xml文件来实现不同的样式:
只需要修改button_style文件,同样三种状态分开定义:
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<gradient android:startColor="#0d76e1" android:endColor="#0d76e1"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>

<item android:state_focused="true">
<shape>
<gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7"
android:angle="270" />
<stroke android:width="1dip" android:color="#f403c9" />
<corners android:radius="州源伏2dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>

<item>
<shape>
<gradient android:startColor="#000000" android:endColor="#ffffff"
android:angle="180" />
<stroke android:width="1dip" android:color="#f403c9"册携 />
<corners android:radius="5dip" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</裂做shape>
</item>
</selector>

gradient 主体渐变 startColor开始颜色,endColor结束颜色 ,angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
stroke 边框 width 边框宽度,color 边框颜色
corners 圆角 radius 半径,0为直角
padding text值的相对位置

⑸ 如何在android style文件中使用自定义属性

在android style文件中使用自定义属性是为了方便,只需要这里写一次就可以在布局文件中多次调用,使用方法如下图:

1、首先使用android studio打开一个项目,如下图:

热点内容
安卓手机为什么最新微信安装不了 发布:2025-02-05 02:31:03 浏览:106
安卓手机什么时候开售 发布:2025-02-05 02:14:15 浏览:660
编程车模型 发布:2025-02-05 02:09:55 浏览:680
雅马哈天剑哪个配置好 发布:2025-02-05 02:00:35 浏览:170
我的世界国际服推荐118服务器 发布:2025-02-05 01:50:48 浏览:46
普通电脑做服务器怎么操作 发布:2025-02-05 01:46:22 浏览:628
原神为什么同服务器加不起好友 发布:2025-02-05 01:41:03 浏览:337
android连接打印机 发布:2025-02-05 01:40:09 浏览:959
外国电脑代理服务器地址端口 发布:2025-02-05 01:38:30 浏览:387
德意龙鼠标宏怎么配置 发布:2025-02-05 01:34:13 浏览:318