当前位置:首页 » 安卓系统 » android设置style

android设置style

发布时间: 2023-03-26 20:00:49

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

当然可以喽,可以在onCreate的时候就设置,setStyle()

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

如果你是给textView的话镇宏,可以

textView.setTextAppearance(this, R.style.MyStyle);

其它的耐圆话就不行了昌旅塌。

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

<style name="text_style">
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>
xml使用:
<TextView
style="@style/button_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</TextView>
java代码使用:
TextView txtname = new TextView(this);
txtname.setTextAppearance(this, R.style.text_style);

④ 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吗

可明侍以的,步骤如下:

  1. 在项目的values/styles.xml文件里定义style属性


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

可以的,步骤如下:

1、在项目的岁悔values/styles.xml文件里定义锋雀困style属性。

⑦ 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 style文件中使用自定义属性

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

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

⑨ 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吗

可以,当然可以喽,可以在onCreate的时候就设置,setStyle() 无论换背景、颜色、字体什么的 首先要得到被换的component, 比如字体,假设他是TextView: 1、得到这个TextView component:TextView tv = (TextView)findViewById(R.id.tv);

热点内容
车辆参数配置包括什么 发布:2025-05-14 21:31:03 浏览:162
怎么引入安卓项目 发布:2025-05-14 21:26:39 浏览:823
游戏辅编程 发布:2025-05-14 21:18:49 浏览:686
三菱plc一段二段密码什么意思 发布:2025-05-14 21:17:16 浏览:527
电脑开机密码忘记了怎么破解 发布:2025-05-14 21:09:40 浏览:56
pythondict格式 发布:2025-05-14 21:09:38 浏览:885
落叶片拍摄脚本 发布:2025-05-14 20:40:49 浏览:798
安卓为什么不能用cmwap 发布:2025-05-14 20:40:43 浏览:657
jquery获取上传文件 发布:2025-05-14 20:27:57 浏览:44
云web服务器搭建 发布:2025-05-14 20:25:36 浏览:526