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

Android-系统换肤的几种方法

时间:2015-07-09 09:44:34      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:android   换肤   

Android-系统换肤的几种方法
一 使用Theme进行简单的换肤
1,为不同的皮肤编写不同的Theme,然后在manifest文件的Activity中应用即可

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

2,在onCreate中动态的设置setTheme

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //设置主题
        setTheme(R.style.AppTheme);
        setContentView(R.layout.layout_portrait);

    }
}

二 改变界面的布局文件来换肤
1,为不同的皮肤编写不同的布局文件

2,加载布局文件

3,重新绑定界面控件

下面是一个横竖屏转换的Demo:
主Activity类:

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState); 
        //初始化状态的竖屏
        setContentView(R.layout.layout_portrait);
        //由打出的log可以判断系统利用onConfigurationChanged转换的时候,不会重新onCreate,只是在第一次
        //的时候onCreate
        log.i("chengzhi log", "onCreate");
    }

    //利用onConfigurationChanged可以提高转换的效率
    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);

        //系统状态改变为垂直的时候
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            //设置布局为竖屏
            setContentView(R.layout.layout_portrait);

        }
        //系统状态改变为水平的时候
        else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            //设置布局为水平
            setContentView(R.layout.layout_landscape); 
        }
         //打出log,判断是否正常运行
        Log.i("chengzhi", "onConfigurationChanged");
    }  
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

两个布局文件类:
1,水平布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    android:orientation="horizontal" >
   //四个按钮
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

如下图:
技术分享

2,竖直布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    //下面是四个按钮
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

如下图:
技术分享

manifest文件注册主Activity:

<activity
        android:name="com.example.androidconfigchange.MainActivity"
            android:label="@string/app_name"
            <!--设置configChanges属性为orientation -->
            android:configChanges="orientation" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<!--添加权限  -->
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>

版权声明:欢迎交流指正文章的错误,必定虚心接,QQ872785786

Android-系统换肤的几种方法

标签:android   换肤   

原文地址:http://blog.csdn.net/qq_22075977/article/details/46811971

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