码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-02-17 08:19:26      阅读:390      评论:0      收藏:0      [点我收藏+]

标签:

分类:C#、Android、VS2015;

创建日期:2016-02-17

一、简介

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

二、示例—ch0802ThemeDemo

1、运行截图

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

技术分享  技术分享

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

技术分享  技术分享

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

技术分享  技术分享

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);
        }
    }
}

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

标签:

原文地址:http://www.cnblogs.com/rainmj/p/5194210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!