样式主题与自定义View
样式和主题资源都是用于对Android应用进行美化的。
一、样式:
(一)、介绍:
1、
概念
:Android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是包含一个或者多个view控件属性的集合。如:需要定义字体的颜色和大小。
2、作用:将一些常用的属性组合成样式,便于重复使用,
减少给V
iew控件指定类似属性的重复工作。
Android Style类似网页设计中的级联样式CSS设计思路,可以让设计与内容分离,并且可以方便的继承、覆盖、重用。
(二)、用法:
1、存储位置:
res/values目录下
2、写法:
- 以<recources>为根标签
- 二级节点为<style>标签: 其中包含name属性和parent属性。
- name:指定样式的名称;
- parent:指定该样式所继承的父级样式。当继承于某个父样式,那么该样式就获得父样式定义的全部格式。当然,当前样式也可以覆盖父样式的格式。
- 三级节点为<item>标签
- name: 该属性值标准的控件属性的格式;
- item的文本值为该属性所对应的值。
<resources>
<style name=“itcast”> <!-- 为样式定义一个全局唯一的名字-->
<item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 -->
<item name="android:textColor">#0000CC</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<stylename="mystyle1"parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
3、如何调用资源:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
style="
@style/mystyle1"/>
4、样式的继承:
有两种方式来实现继承:一是通过style的parent属性,二是使用类似CSS中的命名规则来实现。
A、
通过style的
parent
属性继承:
<resources>
<style name="mystyle1">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="mystyle2" parent="mystyle1">
<item name="android:textColor">#00FF00</item>
</style>
</resources>
B、通过style的
命名规则继承:
①继承并修改:
<stylename="CodeFont.Red">
<item name="android:textColor">#FF0000</item>
</style>
②继承并增加属性
<stylename="CodeFont.Red.Big">
<item name="android:textSize">30sp</item>
</style>
③引入系统style
parent="@android:style/TextAppearance.Medium"
通过“
.”号实现继承。
引用方式:style="@style/
CodeFont.Red.Big
"
二、主题:
(一)、概念:
1、主题和样式资源非常相似。也是放
res/values
目录下,也是以resourses作为根节点,style作为二级节点,item作为三级节点。属性设置也相似。
2、
尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。
两者区别在于:
- 主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。而样式都写在Activity的布局中,用在单独的View,如:EditText、TextView等;
- 主题定义的格式应该是改变窗口外观的格式:例如窗口标题、窗口边框等等。
- 如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。(也就是谁最靠近UI控件,谁起作用)
【备注:】
Android系统也定义了一些主题,例如:
<activity android:theme=“@android:style/Theme.Dialog”>,
该主题可以让Activity看起来像一个对话框,如果需要查阅这些主题,可以在文档的reference-->android-->R.style 中查看。
其它自带的主题样式。例如
Theme.Translucent等等。
(二)、用法:
1、自定义Activity主题
<style name="MyTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullScreen">true</item>
<item name="android:windowBackground">@drawable/star</item>
</style>
①在styles.xml中添加主题
<colorname="custom_theme_color">#b0b0ff</color>
<stylename="CustomTheme"parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>
②在AndroidManifest.xml中引用主题
<activityandroid:theme="@style/CustomTheme">
2.使用系统的主题
<activityandroid:theme="@android:style/Theme.Translucent">
【备注:】
Android平台提供了一大堆样式和主题,你完全可以在你的应用程序中使用。你可以在R.style class中找到一份关于所有可使用的样式参考。要使用所有在这份参考中列出的样式,你需要将样式name的下划线换成点号。比如,你可以通过”@android:style/Theme.NoTitleBar“来使用这里列出的Theme_NoTitleBar主题。
三、
Android设置横屏或竖屏:
(一)、全屏:
在Activity的onCreate方法中的setContentView(myview)调用之前添加下面代码 :
- requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
(二)、横屏:
1、做法1:修改Activity的onResume():
@Override
protected void onResume() {
// 设置为横屏if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}super.onResume();
}
2、做法2:在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)
android:launchMode="singleTask" android:screenOrientation="portrait">
3、
判断此时屏幕是横屏还是竖屏的方法:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//横屏
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
//竖屏
}
(三)、竖屏:
要设置成竖屏设置成 SCREEN_ORIENTATION_PORTRAIT