• 021_02UI之自定义主题和样式


      在Android的应用的资源文件中有一个style.xml文件,可以对某个类型的组件指定大致相似的格式,比如字体、颜色、背景色等。这样可以不必为某个View组件去重复指定这些属性,而且有利于后期的代码修改和维护。Style格式可以被多个View组件所使用,被需要这一类样式集合的View组件所使用。

      与样式非常相似,主题资源的xml文件通常也放在/res/values目录下,主题一般为整个应用或者某个Activity的样式,换句话说主题是针对窗体级别或整个应用程序的样式

      主题的设置有两种方式

        (1)在AndroidMainfest.xml中为Activity指定主题

        (2)在Activity创建时调用 setTheme方法给某个Activity添加主题

    /res/values/styles.xml 为主题和两个textView设置的style

     1 <resources>
     2 
     3     <!--
     4         Base application theme, dependent on API level. This theme is replaced
     5         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
     6     -->
     7     <style name="AppBaseTheme" parent="android:Theme.Light">
     8         <!--
     9             Theme customizations available in newer API levels can go in
    10             res/values-vXX/styles.xml, while customizations related to
    11             backward-compatibility can go here.
    12         -->
    13     </style>
    14             
    15     <style name="mystyle">
    16         <item name="android:layout_width">wrap_content</item>
    17         <item name="android:layout_height">wrap_content</item>
    18         <item name="android:textColor">#FF0000FF</item>
    19         <item name="android:textSize">20sp</item>
    20         <item name="android:background">#8800FF00</item>
    21         <item name="android:paddingTop">10dp</item>
    22         <item name="android:paddingRight">20dp</item>
    23         <item name="android:paddingLeft">20dp</item>
    24         <item name="android:paddingBottom">10dp</item>
    25         <item name="android:layout_marginLeft">50dp</item>
    26         <item name="android:layout_marginTop">20dp</item>
    27         <item name="android:textStyle">bold</item>
    28     </style>
    29 
    30      <style name="mystyle2"  parent="mystyle">        
    31         <item name="android:textColor">#FF0000</item>    
    32     </style>
    33       
    34      <style name="MyTheme" >
    35          <item name="android:colorBackgroundCacheHint">@null</item>
    36         <!--<item name="android:windowIsTranslucent">true</item>
    37           <item name="android:windowNoTitle">true</item>
    38          <item name="android:windowFullscreen">true</item> -->
    39         <item name="android:windowBackground">@drawable/fun1</item>       
    40      </style> 
    41      
    42 </resources>

    本例采用setTheme方法添加主题

     1 package com.example.day21_02styleandtheme;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.Menu;
     6 import android.view.MenuItem;
     7 
     8 public class MainActivity extends Activity {
     9 
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setTheme(R.style.MyTheme);
    14         setContentView(R.layout.activity_main);
    15     }
    16 }
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.example.day21_02styleandtheme.MainActivity" 
    10     android:orientation="vertical">
    11 
    12     <TextView
    13         style="@style/mystyle2"
    14         android:text="功能1" />
    15      <TextView
    16         style="@style/mystyle"
    17         android:text="功能2" />          
    18 </LinearLayout>

    效果图:

    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    Hibernate框架配置
    单利模式的数据库工具类
    EL
    JSP内置对象---request和 response
    模拟淘宝使用cookie记录登录名,
    模拟操作银行登入页面。
    jsp...九九乘法表,三角形,菱形
    jdbc模拟电话本。
    22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。
    机动车
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4547713.html
Copyright © 2020-2023  润新知