• 【Android】8.2 动态选择和设置主题


    分类:C#、Android、VS2015;

    创建日期:2016-02-17

    一、简介

    除了通过Theme指定主题外,还可以在程序运行时动态指定并应用主题。

    二、示例—ch0802ThemeDemo

    1、运行截图

    下面左图:活动条(ActionBar)也是浅色的;右图:没有活动条

    image  image

    下面左图:全屏不带活动条;右图:带活动条的黑色主题

    image  image

    下面左图:不带活动条的黑色主题;右图:带墙纸的材料主题

    image  image

    2、相关代码

    (1)ch0802_ThemeDemo.axml文件

    在Resources/layout文件夹下添加该文件。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:text="提示:单击主题可立即看到当前页面的应用效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView1"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:textSize="14dp"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="5dp" />
        <ListView
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView1"
            android:listSelector="@color/myGreen"
            android:choiceMode="singleChoice" />
    </LinearLayout>

    (2)ch0802ThemeDemo.cs文件

    在SrcDemos文件夹下添加该文件。

    using System.Collections.Generic;
    using Android.App;
    using Android.OS;
    using Android.Widget;
    namespace MyDemos.SrcDemos
    {
        [Activity(Label = "【例8-2】动态选择和设置主题")]
        public class ch0802ThemeDemo2 : Activity
        {
            private int slectedThemeId;
            protected override void OnCreate(Bundle savedInstanceState)
            {
                if (savedInstanceState != null)
                {
                    SetTheme(savedInstanceState.GetInt("theme_id"));
                }
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.ch0802_ThemeDemo2);
    
                //这里仅列出一部分Android内置的主题,其它的自己试吧
                List<int> themesId = new List<int>() {
                    Android.Resource.Style.ThemeDeviceDefaultLightDarkActionBar,
                    Android.Resource.Style.ThemeDeviceDefaultLight,
                    Android.Resource.Style.ThemeDeviceDefaultLightNoActionBar,
                    Android.Resource.Style.ThemeDeviceDefaultLightNoActionBarFullscreen,
    
                    Android.Resource.Style.ThemeDeviceDefault,
                    Android.Resource.Style.ThemeDeviceDefaultNoActionBar,
    
                    Android.Resource.Style.ThemeMaterialWallpaper,
                };
                List<string> themesName = new List<string>();
                foreach (var v in themesId)
                {
                    themesName.Add(Theme.Resources.GetResourceEntryName(v));
                }
                var listView1 = FindViewById<ListView>(Resource.Id.listView1);
                listView1.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemSingleChoice, themesName.ToArray());
                //让字号小一些,以便能一行显示一个主题
                listView1.ChildViewAdded += (s, e) =>
                {
                    ((TextView)e.Child).TextSize = 12;
                };
                //演示如何设置默认选项
                listView1.SetItemChecked(0, true);
                //单击某个主题项引发的事件
                listView1.ItemClick += (s, e) =>
                {
                    slectedThemeId = themesId[e.Position];
                    //重新创建该页,此方法会自动调用OnSaveInstanceState方法
                    Recreate();
                };
            }
    
            protected override void OnSaveInstanceState(Bundle outState)
            {
                //将当前所选的主题传递给新实例
                outState.PutInt("theme_id", slectedThemeId);
                base.OnSaveInstanceState(outState);
            }
        }
    }
  • 相关阅读:
    Python--day68--ORM内容回顾
    Python--day67--include包含其他的url和反向解析URL
    Python--day67--Django的路由系统
    Python--day67--Jsonresponse响应介绍和路由系统的分组命名匹配方式(简单介绍)
    Python--day67--CBV和FBV、Request对象及上传文件示例
    Python--day66--Django模板语言关于静态文件路径的灵活写法
    GET和POST两种基本请求方法的区别
    ASP.NET中使用UpdatePanel实现局部异步刷新方法和攻略(转)
    GridView中实现DropDownList联动
    .NET string字符串的截取、移除、替换、插入
  • 原文地址:https://www.cnblogs.com/rainmj/p/5194210.html
Copyright © 2020-2023  润新知