您的位置:首页 > 移动开发 > Android开发

Android-系统换肤的几种方法

2015-07-09 08:45 369 查看
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"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: