分享

Android API Demo研究(1) - 编程 - goandroid

 earthworm 2010-10-09
Android API Demo研究(1)

1. Custom Dialog

Android支持自定义窗口的风格:

1)首先在资源里面建立style的value;

example:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/filled_box</item>
</style>

drawable/filled_box.xml

<shape xmlns:android="http://schemas./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>

PS:关于Styles的学习,可以参见:http://code.google.com/android/reference/available-resources.html#stylesandthemes

2)设置当前activity的属性,两种方式:1.在manifest文件中给指定的activity增加属性

android:theme="@android:style/Theme.CustomDialog"。2.在程序中增加语句setTheme(R.style.Theme_CustomDialog);

PS1:如果只是将Acticity显示为默认的Dialog, 跳过第一步,只需要在manifest文中增加属性:android:theme="@android:style/Theme.Dialog"或者在程序中增加setTheme(android.R.style.Theme_Dialog).

PS2:其他创建Dialog的方法:创建app.Dialog类或者创建app.AlertDialog类。

Next Study:能不能在Activity已经打开以后动态修改当前Activity的风格?

在测试中发现,在onCreate()事件中增加setTheme(),必须在setContentView()之前,否则指定的Style不能生效

2.Custom Title

Android除了可以为指定的Activity设置显示风格,此外也可以为指定的Activity设置一些特效,比如自定义Title,没有Title的Activity或者增加一个ICON等。

有意思的一点是,这些特效并不是你想设置的时候就行设置,你需要在Activity显示之前向系统申请要显示的特效,这样才能在下面的程序中为这些特效进行设置。(这样是不是多此一举有待研究)

为一个Activity设置自定义Title的流程:

1)为自定义的Title建立一个layout(custom_title_1.xml)

<RelativeLayout xmlns:android="http://schemas./apk/res/android" android:id="@+id/screen"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Left" />
    <TextView android:id="@+id/right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Right" />
</RelativeLayout>

关于为什么采用RelativeLayout,可以参见:http://code.google.com/android/devel/ui/layout.html

2)为activity设定自定义Title特效并指定Title的layout:

在onCreate()事件中增加:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.custom_title);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);

这三条语句的次序不能颠倒,依次为申请特效,创建view,设置特效属性。其中requestWindowFeature等价于getWindow().requestFeature()

3)在需要修改Title的地方,获取left_text或者right_text进行设置即可。

Next Study:Activity的其他显示特效

Window还有其他一些feature,比如FEATURE_CONTEXT_MENU,FEATURE_NO_TITLE,FEATURE_LEFT_ICON等,有待继续学习研究。

 原文地址 http://zxshean.spaces./blog/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多