Android UI中的style和theme以及AlertDialog
来源:互联网 发布:小马搜索软件下载 编辑:程序博客网 时间:2024/05/16 11:19
1.sytle:Android中的style就是用简单的配置来实现页面的外观和风格的方式。他是一个包含一个或者多个view控件属性的集合,可以当成一个整体应用到XML单个元素上。
例如我们可以在res/values/styles.xml文件,键入代码:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="style1"> <!-- 为样式定义一个全局唯一的名字-->
- <item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 -->
- <item name="android:textColor">#0000CC</item>
- <!--
- 等等……
- -->
- <item name="android:layout_width">60dip</item>
- <item name="android:layout_height">50dip</item>
- <item name="android:layout_weight">1</item>
- <item name="android:divider">#FFCFCFCF</item>
- <item name="android:dividerHeight">0.5dip</item>
- <item name="android:listSelector">@drawable/list_item_bg</item>
- <item name="android:cacheColorHint">#00000000</item>
- </style>
- <style name="TitleStyle">
- <item name="android:textSize">18sp</item>
- <item name="android:textColor">#ec9237</item>
- </style>
- <style name="LinkStyle">
- <item name="android:textSize">18sp</item>
- <item name="android:textColor">#ec0032</item>
- <item name="android:fromAlpha">0.0</item>
- <item name="android:toAlpha">0.0</item>
- </style>
- <style name="SpecialText">
- <item name="android:textSize">28sp</item>
- <item name="android:textColor">@color/darkgreen</item>
- <item name="android:gravity">center</item>
- <item name="android:textStyle">bold|italic</item>
- <item name="android:background">@drawable/icon</item>
- </style>
- </resources>
<?xml version="1.0" encoding="utf-8"?><resources><style name="style1"> <!-- 为样式定义一个全局唯一的名字--><item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 --><item name="android:textColor">#0000CC</item><!--等等……--><item name="android:layout_width">60dip</item><item name="android:layout_height">50dip</item><item name="android:layout_weight">1</item><item name="android:divider">#FFCFCFCF</item><item name="android:dividerHeight">0.5dip</item><item name="android:listSelector">@drawable/list_item_bg</item><item name="android:cacheColorHint">#00000000</item></style><style name="TitleStyle"><item name="android:textSize">18sp</item><item name="android:textColor">#ec9237</item></style><style name="LinkStyle"><item name="android:textSize">18sp</item><item name="android:textColor">#ec0032</item><item name="android:fromAlpha">0.0</item><item name="android:toAlpha">0.0</item></style><style name="SpecialText"><item name="android:textSize">28sp</item><item name="android:textColor">@color/darkgreen</item><item name="android:gravity">center</item><item name="android:textStyle">bold|italic</item><item name="android:background">@drawable/icon</item></style></resources>
这其实也就是把view的属性罗列出来,用一个view关联。这样在遇到view配置相同的属性的时候就可以直接通过这个名字直接关联这样的样式设置了。其实这个还想是WEB开发中的CSS的使用。就是把view的属性单独写出,提高重用性。
在layout文件中可以像下面这样使用上面的android样式:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView style="@style/TitleStyle"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical|center_horizontal"
- android:text="txlong_onz ai slna" />
- <TextView style="@style/LinkStyle"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical|center_horizontal"
- android:text="http://txlong-onz.iteye.com"
- android:autoLink="all" />
- <TextView style="@style/SpecialText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical|center_horizontal"
- android:text="SpecialText" />
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView style="@style/TitleStyle" android:layout_width="fill_parent"android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal"android:text="txlong_onz ai slna" /><TextView style="@style/LinkStyle" android:layout_width="fill_parent"android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal"android:text="http://txlong-onz.iteye.com" android:autoLink="all" /><TextView style="@style/SpecialText" android:layout_width="fill_parent"android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal"android:text="SpecialText" /></LinearLayout>
<style>标签中有一个parent属性。这个属性可以让当前样式继承一个父样式,当前样式可以继承到父样式的值。当然,如果父样式的值不符合你的需求,你也可以对它进行修改,和CSS中的覆盖效果一样,都是以最后的为准,例子如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="style1">
- <item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 -->
- <item name="android:textColor">#0000CC</item>
- </style>
- <style name="subitcast" parent="@style/style1">
- <item name="android:textColor">#FF0000</item>
- </style>
- </resources>
<?xml version="1.0" encoding="utf-8"?><resources><style name="style1"><item name="android:textSize">18px</item> <!-- name属性为样式要用在的View控件持有的属性 --><item name="android:textColor">#0000CC</item></style><style name="subitcast" parent="@style/style1"><item name="android:textColor">#FF0000</item></style></resources>
样式的继承,一种是继承平台自带的。一种是继承自定义的样式。继承平台原有的一定要parent指定。
- <style name="GreenText" parent="@android:style/TextAppearance">
- <item name="android:textColor">#00FF00</item>
- </style>
<style name="GreenText" parent="@android:style/TextAppearance"><item name="android:textColor">#00FF00</item></style>
继承自定义的只需在name前加要继承的style主题就行了。<style name="GreenText.Red">
- <item name="android:textColor">#FF0000</item>
- lt;/style>
<item name="android:textColor">#FF0000</item></style>
这种方法可以不断地继承
- <style name="CodeFont.Red.Big">
- <item name="android:textSize">30sp</item>
- </style>
<style name="CodeFont.Red.Big"><item name="android:textSize">30sp</item></style>
2.theme:Android主题设置其实就是将主题添加到一个配置里,以方便调用,提高重用性。所以配置文件的属性也就是窗口等的主题样式。
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="theme1">
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowFullscreen">?android:windowNoTitle</item>
- </style>
- </resources>
<?xml version="1.0" encoding="utf-8"?><resources><style name="theme1"><item name="android:windowNoTitle">true</item><item name="android:windowFullscreen">?android:windowNoTitle</item></style></resources>
上面"?android:windowNoTitle"中的问号用于引用在当前主题中定义过的资源的值。下面代码显示在AndroidManifest.xml中如何为应用设置上面定义的主题:
- <application android:icon="@drawable/icon" android:label="@string/app_name"
- android:theme="@style/theme1">
- <activity android:name=".MessageShowActivity" android:label="@string/app_name"
- android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"
- android:theme="@style/theme2">
- </activity>
- </application>
<application android:icon="@drawable/icon" android:label="@string/app_name"android:theme="@style/theme1"><activity android:name=".MessageShowActivity" android:label="@string/app_name"android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"android:theme="@style/theme2"></activity></application>
除了可以在AndroidManifest.xml中设置主题,同样也可以在代码中设置主题,如下:
setTheme(R.style.theme1);
尽管在定义上,样式和主题基本相同,但是它们使用的地方不同。样式用在单独的View,如:EditText、TextView等;主题通过AndroidManifest.xml中的<application>和<activity>用在整个应用或者某个 Activity,主题对整个应用或某个Activity存在全局性影响。如果一个应用使用了主题,同时应用下的view也使用了样式,那么当主题与样式属性发生冲突时,样式的优先级高于主题。
另外android系统也定义了一些主题,例如:
<activity android:theme="@android:style/Theme.Dialog">,该主题可以让Activity看起来像一个对话框,
<activity android:theme="@android:style/Theme.Black.NoTitleBar">Variant of the light theme with no title bar,系统自带的黑色主题。如果需要查阅这些主题,可以在文档的reference-->android-->R.style 中查看。
3.AlertDialog的使用Demo:
主要的类在这里粘出来,style和theme自己下载下边的附件。
- public class StyleThemeTest extends Activity {
- public final String TAG = "Test";
- private Dialog mDialog;
- private Button button1;
- private Button button2;
- private Button button3;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- StyleThemeTest.this.setTheme(R.style.theme1);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button1 = (Button) findViewById(R.id.button1);
- button2 = (Button) findViewById(R.id.button2);
- button3 = (Button) findViewById(R.id.button3);
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- mDialog = new AlertDialog.Builder(StyleThemeTest.this)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Warning!")
- .setMessage("Are you sure to Follow or UnFollow the friend?")
- .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- Log.v(TAG, "OK");
- }
- })
- .setNeutralButton("Cancel",new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- Log.v(TAG, "Cancale");
- }
- }).create();
- mDialog.show();// 如果要显示对话框,一定要加上这句
- }
- });
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Builder builder = new AlertDialog.Builder(StyleThemeTest.this);
- builder.setIcon(android.R.drawable.alert_dark_frame);
- builder.setTitle("AlertDialog Title.");
- builder.setMessage("What do think about this?");
- builder.setNegativeButton("好", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Log.v(TAG, "good");
- }
- });
- builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Log.v(TAG, "just soso");
- }
- });
- builder.setPositiveButton("不好", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Log.v(TAG, "bad");
- }
- });
- builder.create().show();
- }
- });
- button3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- new AlertDialog.Builder(StyleThemeTest.this)
- .setMessage("There is a xxxxxxxxxxxx error hanppened!!!")
- .setPositiveButton("知道了!", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- Log.v(TAG, "I KNOW.");
- }
- }).create().show();
- }
- });
- }
- }
public class StyleThemeTest extends Activity {public final String TAG = "Test";private Dialog mDialog;private Button button1;private Button button2;private Button button3;@Overridepublic void onCreate(Bundle savedInstanceState) {StyleThemeTest.this.setTheme(R.style.theme1);super.onCreate(savedInstanceState);setContentView(R.layout.main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button3 = (Button) findViewById(R.id.button3);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mDialog = new AlertDialog.Builder(StyleThemeTest.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Warning!").setMessage("Are you sure to Follow or UnFollow the friend?").setPositiveButton("Ok", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Log.v(TAG, "OK");}}).setNeutralButton("Cancel",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Log.v(TAG, "Cancale");}}).create();mDialog.show();// 如果要显示对话框,一定要加上这句}});button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Builder builder = new AlertDialog.Builder(StyleThemeTest.this);builder.setIcon(android.R.drawable.alert_dark_frame);builder.setTitle("AlertDialog Title.");builder.setMessage("What do think about this?");builder.setNegativeButton("好", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Log.v(TAG, "good");}});builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Log.v(TAG, "just soso");}});builder.setPositiveButton("不好", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Log.v(TAG, "bad");}});builder.create().show();}});button3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {new AlertDialog.Builder(StyleThemeTest.this).setMessage("There is a xxxxxxxxxxxx error hanppened!!!").setPositiveButton("知道了!", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {Log.v(TAG, "I KNOW.");}}).create().show();}});}}
- Android UI中的style和theme以及AlertDialog
- Android UI中的自定义style和theme以及AlertDialog
- Android中的style和theme
- Android中的style和theme
- Android中的Theme和Style
- Android 中的Theme和Style
- Android中的style和theme
- 【Android UI】theme style
- Android Theme和Style切换UI
- Android UI–Style 和 Theme
- Android中的主题(theme)和样式(style)
- Android中的style和theme的用法
- 说说Android中的style和theme
- Android中的Attr、Style和Theme详解
- Android 中的Theme和Style使用
- Android 中的style和Theme的使用
- Android:Theme 和 style
- android style和theme
- tomcat install for linux
- Subversion配置安装-锦集
- Android之ContentProvider总结
- gridcolumn获取列头标题!
- 计算机专业经典书籍(汇总)
- Android UI中的style和theme以及AlertDialog
- java发送post请求并接收返回
- Tomcat目录映射设置
- 人间词话 王国维
- java高并发-静态页面生成方案(2)
- window.open()打开的窗口第2次进入时不执行page_load
- error: 'struct passwd' has no member named 'pw_comment'
- C/C++面试感受和经验以及面试题收藏
- android Bitmap用法总结