`
007007jing
  • 浏览: 41271 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

android2.3 api demo 学习系列(3)--App/Activity/Custom Dialog

阅读更多

apidemos里面展示的自定义窗口其实并不复杂,下面我们来看一下:

1、CustomDialogActivity和别的activity并没有区别

2、activity在项目AndroidManifest.xml配置文件中加入 android:Theme属性 例如示例:

 

<activity android:name=".app.activity.CustomDialogActivity" android:label="@string/app_activity_custom_dialog_lable"
              android:theme="@style/Theme.CustomDialog">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.angie.apidemos.SAMPLE_CODE" />
            </intent-filter>
</activity>

  3、接下来就是创建theme Theme.CustomDialog。定义Theme 和定义Style 一样, 必须定义在/res/values 子目录下,

 

根元素名为resources,Theme 和Style 的区别在于Theme 应用于Activity 和Application 而 Style 应用于单个的View。

其定义方法是一致的。Style 定义支持 Inheritance, 也就是在定义新风格时可以基于系统定义的风格或是之前定义的风格: 如Theme.CustomDialog 定义就是基于Android 的Dialog 风格(parent)而只修改的WindowsBackground 属性,使用了褐色背景。

定义的theme如下:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/app_activity_custom_dlg_filled_box</item>
    </style>
</resources>

 

  其中引用了drawable里面的xml文件 其定义的是具体的样式 例如:

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#f0600000"/>
    <stroke android:width="3dp" color="#ffff8080"/>
    <corners android:radius="3dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

 

 效果如如下:

SDK中关于Styles and Themes的说明:

样式是规定 VIew和window显示样式的集合. 样式可以指定的属性: height, padding, font color, font size, background color等等.在layout XML文件中我们经常见到下面的定义:

 

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

 

   同样我们可以把textview的样式属性单独定义带CodeFont.xml中,在layout中引用如下:

 

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

 主题(theme)是应用到整个 Activity 或者 application的样式, 不是指单个view的样式定义. 如果将一个样式作为主题使用,那么在Activity或者application中得每一个view都会被主题样式影响。比如:CodeFont作为主题应用到Activity或者application,Activity或者application中得text都会变成green monospace font

 

定义Styles

styles的定义文件xml必须保存于 项目res/values/ 中,并且文件的根节点必须是<resources>,类似的定义如下

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>
 

 

 注:theme的定义和style一样,主要是看你将其应用在view上还是activity或者application上;

 

同时style的定义支持继承,例如我们定义一个新的style继承自系统已有的style

 

<style name="GreenText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#00FF00</item>
    </style>

 新的GreenText继承自系统已有style TextAppearance,只是修改了文字的颜色。

需要格外注意的是:我们继承系统已有style的时候使用了关键字parent,但是如果需要继承我们自己定义的style的时候就不能使用parent关键字了,sdk中介绍使用 点 举例说明:继承我们自己定义的CodeFont style

 

<style name="CodeFont.Red">
        <item name="android:textColor">#FF0000</item>
    </style>

 

  新的style在使用的时候可以直接使用:@style/CodeFont.Red. 我们可以继续继承新的style来定义style

 

<style name="CodeFont.Red.Big">
        <item name="android:textSize">30sp</item>
    </style>

  新的style继承了CodeFont 和 CodeFont.Red 的所有属性,并且新定义的字体大小属性。

 

 

特别强调:这种使用小数点方式的继承机制只使用于自定义的资源,如果使用android内置的style或者theme必须使用关键字parent

关于style properties建议参考sdk中每个view class 。里面有详细的说明该view支持的属性。

关于已定义的style引用详细清单请参考sdk中得R.attr。其中以window开头的引用只使用与主题(theme),不适用任何一个view。例如 windowBackground 等;

 

接下来我们举例说明style和theme的用法

1.view element 

 

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

 

 2.view使用已有style

 

<TextView
    style="@style/CodeFont"
    android:text="@string/hello" />

 

  3.将theme应用到activity或者application

 

<application android:theme="@style/CustomTheme">
<activity android:theme="@android:style/Theme.Dialog">

   如果系统的theme并不完全适用于项目,可以像下面这个样子修改theme

 

<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">@color/custom_theme_color</item>
    <item name="android:colorBackground">@color/custom_theme_color</item>
</style>

  然后再使用:

<activity android:theme="@style/CustomTheme">

Select a theme based on platform version

如果想根据不同的平台版本选择主题,加入我们想规定Android 3.0 (API Level 11)或者更高的版本的主题,则可以定义theme.xml 保存在/res/values-v11 文件夹内。

 

<style name="LightThemeSelector" parent="android:Theme.Holo.Light">
    ...
</style>

 注意parent引用的时候使用的引用名称为全称。

 

 

至此我们简单的了解了android的style和theme 更多详细的内容大家还是参考sdk中得说明。谢谢

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 42.6 KB
分享到:
评论

相关推荐

    android dialog------普通对话框

    android dialog-------普通对话框

    Android代码-Material Design风格Preference UI

    implementation 'moe.shizuku.preference:preference-dialog-android:' //implementation 'moe.shizuku.preference:preference-dialog-appcompat:' // if you want to use appcompat version dialog //...

    Android代码-AppUpdater版本更新、一键傻瓜式升级App

    AppUpdater for Android 是一个专注于App更新一键傻瓜式集成的开源库,主要包括app-updater和app-dialog。 &gt; 下载更新和弹框提示分开,是因为这本来就是两个逻辑。完全独立开来能有效的解耦。 app-updater 主要负责...

    bootstrap3-dialog 基于bootstrap3的弹出层组件

    * 本js只是在bootstrap3-dialog的基础上进行的2次封装, * bootstrap3-dialog已经是非常好用了,调用也非常简单。 * 详情请访问:https://github.com/nakupanda/bootstrap3-dialog, * 这是原项目地址,可以自行...

    react-native-android-location-services-dialog-box:React本机Android位置服务对话框

    React Native Android Location Services对话框 从Android位置服务打开对话框的React-Native组件安装大多是自动安装(推荐) yarn add react-native-android-location-services-dialog-box 要么npm install react-...

    Element-ui css非网络引用,

    &lt;el-dialog :visible.sync="visible" title="Hello world"&gt; &lt;p&gt;Try Element&lt;/p&gt; &lt;/el-dialog&gt; &lt;/div&gt; &lt;el-button type="primary" icon="el-icon-search"&gt;搜索&lt;/el-button&gt; &lt;!-- import Vue before Element -...

    Android---UI篇---Dialog(对话框)

    Android---UI篇---Dialog(对话框)

    Android自定义dialogDemo

    Android自定义dialogDemo

    Android:自定义Dialog-Demo

    Dialog 很经常用到的一个控件,但大多时候系统默认的样式是不满足于项目使用的,故自定义了一套合适的Dialog模板方便日常使用。

    Android代码-LLollipop dialog demo

    A simple to use library to create a android L dialog on any 2.3 android device in your app using 3 Lines of code! How to compile the example Download the zip Extract Import with android studio ...

    vue-dialog-drag-可拖动对话框-Vue.js开发

    vue-dialog-drag简单的可拖动对话框演示功能:拖放支持(仅用于拖放,不用于拖放)拖放区域组件“ Pin模式”,以锁定vue-dialog-drag简单的可拖动对话框演示功能:拖放支持(仅用于拖动,不用于放置)放置区域组件...

    Android代码-jmdsp

    You'll need something that handles "org.openintents.action.PICK_FILE" ....Install my app: ...- Tap "Load" text to open an open file dialog. Pick a SMF file. -

    Android代码-Range-Time-Picker-Dialog

    Simple Android Library that provide you a custom dialog that allow you to set a start time and end time. Screenshot Install Add this to your project build.gradle allprojects { ...

    API对话框------Win32 DialogBased Application插件的安装

    API对话框------Win32 DialogBased Application插件的安装

    Android Custom Dialog Demo

    在Android开发中,我们经常会需要在android界面上弹出一些对话框,比如询问用户或者让用户选择。,但是只是给出了创建各种类型对话框的方法,并未给出如何获取用户在对话框中做出选择后的数据,即如何监听用户做出的...

    Android代码-Dialog

    本例中,包含DialogDemo(Dialog/app/)是对话框的演示项目源代码,以及Library库(Dialog/dialog/)是封装的空祖家对话框的源代码。 项目托管的Maven仓库在https://bintray.com/myzchh/maven/dialog 本项目遵循...

    Android代码-Simply Solid

    Material color chooser dialog; Save colors to storage; Round icon support for android 7.1; The app supports 4.1 devices. You can download the latest (3.0.6) signed APK from this repo here: ...

    jquery-ui-dialog-demo

    jquery 弹出窗口插件,包含js中的 alert confirm open 等。

    m-dialog:vue的对话框组件

    Dialog Vue 的 Dialog 弹窗组件,包含了 alert 和 confirm 对话框。 参考代码: Use npm install vue-m-dialog import MDialog from 'vue-m-dialog' // ro import { Dialog, Alert, Confirm } from 'vue-m-dialog' ...

    Activity替代dialog的demo

    Activity替代dialog的demo,Activity替代dialog的demo,Activity替代dialog的demo

Global site tag (gtag.js) - Google Analytics