对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
原创粉丝点击