对Android APP的各种样式统一修改方法(这里列举了通知状态栏StatueBar,标题栏ActionBar,ToolBar和按钮Button的样式修改)
来源:互联网 发布:数据库小图标 编辑:程序博客网 时间:2024/05/16 11:44
有些时候为了UI美化和主题的统一,需要我们对APP的主题样式进行修改,比如修改状态栏,ActionBar 的背景色,设置能对特定控件的样式进行自定义,就不必要我们在每个创建控件中去修改他的样式,大大减少代码量和时间,是一个高效的方法
废话不多说,直接上代码
- 首先,在项目中找到并打开AndroidManifest.xml文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.a14392.style"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
android:theme=”@style/AppTheme”是APP的主题,有些activity会设置theme主题,这点要注意看清楚,忽略这个问题可能导致个别activity的样式没有得到修改,修改别的主题的样式,方法也一样。略过。。。
- 其次,在values文件夹的style文件里修改APP主题的样式
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item><!--整个activity的背景色--> <item name="colorPrimaryDark">#0d18ef</item><!--通知栏的颜色--> <item name="colorAccent">@color/colorAccent</item><!--控件的选中颜色--> <item name="actionBarStyle">@style/ActionBar</item><!--对ActionBar的样式进行修改--> <item name="toolbarStyle">@style/ToolBar</item><!--对toolBar的样式进行修改--> <item name="buttonStyle">@style/Button</item> </style> <!--通过引入drawable的样式文件,对整体APP的ActionBar样式进行修改--> <style name="ActionBar" parent="Widget.AppCompat.ActionBar"> <item name="background">@drawable/style_file</item> </style> <!--对toolBar的样式进行修改--> <style name="ToolBar" parent="Widget.AppCompat.Toolbar"> <item name="android:background">@drawable/style_file</item> </style> <!--对button进行修改--> <style name="Button" parent="Widget.AppCompat.Button"> <item name="android:background">@drawable/button_style</item> </style>
在AndroidManifest.xml文件中的主题是AppTheme,就在style文件里找到AppTheme,它继承了Theme.AppCompat.Light.DarkActionBar主题,在这个主题样式文件下修改它的样式,如要修改某个控件的样式,则@style/控件样式,另外在style文件中定义该控件的样式,如Button的样式:
<!--对button进行修改-->
<style name="Button" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/button_style</item>
</style>
注意:在android4.4及以下的版本不能用<item name="colorPrimaryDark">#0d18ef</item><!--通知栏的颜色-->
的方式对通知状态栏进行修改.
- 再者,还需要对样式进行定义,这里以Button为例,修改背景,在res资源文件夹的drawable里新建一个资源文件buuton_style.xml,对Button样式进行自定义,新建方法:右键drawable/new/Android Resource File
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <gradient android:startColor="#0652f7" android:centerColor="#4898f4" android:endColor="#21b7fc"/><!--渐变色--> <corners android:radius="10dp"/><!--圆角--> <stroke android:color="#4207f2" android:width="1dp"/><!--边距--> </shape> </item></selector>
- 这样对主题的修改基本完成了,我们可以新建一个Activity看一下效果,activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.a14392.style.MainActivity"> <android.support.v7.widget.Toolbar android:layout_marginTop="25dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:layout_marginTop="25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一个定制的Button" android:layout_gravity="center"/></LinearLayout>
有图有真相:
我不是大神,有些地方可能写的不对,如有问题,请在下面评论指出,万分感谢!
阅读全文
0 0
- 对Android APP的各种样式统一修改方法(这里列举了通知状态栏StatueBar,标题栏ActionBar,ToolBar和按钮Button的样式修改)
- 修改Actionbar的样式
- 修改actionBar的样式
- Android Actionbar学习笔记(三)-----修改Actionbar的样式
- Android Actionbar学习笔记(三)-----修改Actionbar的样式
- Android Actionbar学习笔记(三)-----修改Actionbar的样式
- Android Actionbar学习笔记(三)-----修改Actionbar的样式
- Android Actionbar学习笔记(三)-----修改Actionbar的样式
- Android 开发------------------ 修改 Actionbar 的样式
- 修改按钮的样式
- Android 修改ActionBar样式
- android状态栏样式修改
- Android修改状态栏样式
- 修改活动窗口标题栏的样式和标题栏字体
- 修改ActionBar的样式(overflow按钮图片修改,AcitonBar背景颜色,去掉ActionBar下面的阴影)
- Android button样式修改
- 自定义主题样式,修改标题栏,状态栏;
- 修改file按钮的样式
- aix的lvm管理
- 【bzoj1146】 [CTSC2008]网络管理Network
- STL常用算法
- 戴尔XPS 13 Ubuntu 16.04 Chrome浏览器闪屏问题解决办法
- 确定的有限自动机VS非确定的有限自动机(DFANFA)
- 对Android APP的各种样式统一修改方法(这里列举了通知状态栏StatueBar,标题栏ActionBar,ToolBar和按钮Button的样式修改)
- 我的第一次div布局
- Ubuntu16.04LTS 安装搜狗输入法
- clang 在Ubuntu上的配置
- Android onClick 按钮单击事件 四种常用写法
- hdu 1847 Good Luck in CET-4 Everybody!(基础博弈)
- poj3565-Ants
- 1.oracle RAC11G 对单机ADG搭建详细文档
- hihocoder week171分析---并查集