Android-系统换肤的几种方法
2015-07-09 08:45
369 查看
Android-系统换肤的几种方法
一 使用Theme进行简单的换肤
1,为不同的皮肤编写不同的Theme,然后在manifest文件的Activity中应用即可
2,在onCreate中动态的设置setTheme
二 改变界面的布局文件来换肤
1,为不同的皮肤编写不同的布局文件
2,加载布局文件
3,重新绑定界面控件
下面是一个横竖屏转换的Demo:
主Activity类:
两个布局文件类:
1,水平布局
如下图:
2,竖直布局
如下图:
manifest文件注册主Activity:
一 使用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"/>
相关文章推荐
- 小猪Android越来越方式 Day 5 - part 2
- Android-循环广告位组件
- Android-风格和主题
- Android 手机影音 开发过程记录(五)
- Android_Gson/JSON详解
- Android手机安全卫士(一)---概述、启动、签名、主界面、自定义控件
- android悬浮窗口的实现
- Android 自定义DialogFragment替代popupwindow
- Android项目没提示语法错误却运行不了的问题
- Android 动画之TranslateAnimation应用详解
- android中程序的退出和关闭
- android中的surfaceview控件
- Robotium in Android Studio
- android XML布局大全
- Android开发者的Git&GitHub(二)
- Android根据uri获取图片的路径(4.4+)
- android入门学习-java面向对象基础
- Android入门(38)——第十五章 Android布局优化
- Android DiskLruCache完全解析
- Android-DrawerLayout介绍