目录html
样式:java
主题:android
国际化(本地化):ios
对话框:c++
帧动画:chrome
没用样式以前,修改特别麻烦,一旦需求改变,好比TextView颜色不对,字体大小不对,都须要一个个修改TextViewc#
使用样式的好处就是将View的设计和内容分开。数组
关于更多样式讲解建议看官方文档:https://developer.android.google.cn/guide/topics/ui/themes浏览器
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#ff0000" android:text="哈哈哈哈" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#880000" android:text="呵呵呵呵" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000000" android:text="嘿嘿嘿嘿" /> </LinearLayout>
在styles.xml添加这样一段:app
<style name="my_style"> <item name="android:textSize">20sp</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#ff0000</item> </style>
用了样式以后,添加修改只须要在styles.xml文件中修改就能够,因而布局文件改为以下:
<?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"> <TextView style="@style/my_style" android:text="哈哈哈哈" /> <TextView style="@style/my_style" android:text="呵呵呵呵" /> <TextView style="@style/my_style" android:text="嘿嘿嘿嘿" /> <TextView style="@style/my_style" android:text="呼呼呼呼" /> <TextView style="@style/my_style" android:text="嘻嘻嘻嘻" /> </LinearLayout>
效果图:
若是以为字体颜色不对,想要换成黑色,而且最后一个要换成黑底白字,倒数第二个字体变大,那么就把刚刚的styles.xml文件中属性修改掉
<style name="my_style"> <item name="android:textSize">20sp</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#000000</item> </style> <style name="my_bigsize" parent="my_style"> <item name="android:textSize">40sp</item> </style> <style name="my_style.night"> <item name="android:textColor">#ffffff</item> <item name="android:background">#000000</item> </style>
继承父类要么写父类加"."再加子类,用的时候必须和name对应,my_style.night而不是直接night,要么加上parent属性,总之,和style标签的name属性对应。
布局文件修改成:
<?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"> <TextView style="@style/my_style" android:text="哈哈哈哈" /> <TextView style="@style/my_style" android:text="呵呵呵呵" /> <TextView style="@style/my_style" android:text="嘿嘿嘿嘿" /> <TextView style="@style/my_bigsize" android:text="呼呼呼呼" /> <TextView style="@style/my_style.night" android:text="嘻嘻嘻嘻" /> </LinearLayout>
这样就变成了以下效果:
设置样式的方法有两种:
style
属性。<activity>
或 <application>
元素添加 android:theme
属性。
好比,若是你想要输入一个文本呈现斜体而且字体颜色是蓝色,那么你能够为此定义一个样式,可是若是你想要在你的活动中让全部的输入文本都是蓝色字体和斜体,那么你能够定义一个主题。主题也被用来把样式的属性用到应用窗口,好比应用栏或状态栏。
关于主题更多的讲解建议查看官方文档:https://developer.android.google.cn/guide/topics/ui/themes
在styles.xml中添加以下:
<style name="my_theme" parent="Theme.AppCompat"> <item name="android:background">#00f0f0</item> </style>
若是没有继承Theme.AppCompat那么直接运行就会报错:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
而后在清单文件修改:
运行效果:
样式:通常做用在控件上(button,textview)等,做用范围比较小
主题:通常做用于activity或Application结点下,做用范围比较大
共同点是定义的方式是同样的
国家化简称I18N,其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数,对程序来讲,在不修改内部代码的状况下,能根据不一样语言及地区显示相应的界面。
运行效果图:
更多本地化参见官方文档:https://developer.android.google.cn/distribute/marketing-tools/localization-checklist
values-en/strings.xml
<resources> <string name="app_name">Android_Internationalization</string> <string name="hello_world">Hello World!</string> </resources>
values-zh/strings.xml
<resources> <string name="app_name">Android_Internationalization</string> <string name="hello_world">你好 世界!</string> </resources>
MainActivity.java
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(this, getResources().getString(R.string.hello_world), Toast.LENGTH_SHORT).show(); } }
建立不一样国家环境集目录是固定写法,在values以后用“-代号”表示,会自动根据手机系统的语言来寻找显示相应的字符串,那么这个代号在哪里找呢?要么百度,要么小技巧,chrome浏览器没有,用IE或者QQ浏览器能够,看到工具-Internet选项-常规-语言-添加,就能够看到全部语言的代号了。或者直接搜索ISO 639-1。
如图:
标记不该翻译的信息部分
有时候字符串中包含不该被翻译为其余语言的文本。常见的示例包括代码、某个值的占位符、特殊符号或名称。在准备翻译字符串时,请查找并标记应该保留原样而不用翻译的文本,这样翻译人员就不会更改这些内容。
要标记不该翻译的文本,请使用 <xliff:g>
占位符标记。如下示例标记可确保文本“%1$s”在翻译过程当中不会被更改(不然这条消息会被破坏):
<string name="countdown"> <xliff:g id="time" example="5 days>%1$s</xliff:g>until holiday </string>
在声明占位符标记时,请务必添加说明此占位符用途的 ID 属性。若是您的应用稍后会替换占位符值,请务必提供示例属性来讲明预期用途。
如下是其余一些占位符标记的示例:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <!-- Example placeholder for a special unicode symbol --> <string name="star_rating">Check out our 5 <xliff:g id="star">\u2605</xliff:g> </string> <!-- Example placeholder for a for a URL --> <string name="app_homeurl"> Visit us at <xliff:g id="application_homepage">http://my/app/home.html</xliff:g> </string> <!-- Example placeholder for a name --> <string name="prod_name"> Learn more at <xliff:g id="prod_gamegroup">Game Group</xliff:g> </string> <!-- Example placeholder for a literal --> <string name="promo_message"> Please use the "<xliff:g id="promotion_code">ABCDEFG</xliff:g>” to get a discount. </string> ... </resources>
MainActivity.java
import android.content.DialogInterface; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; public ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = (ProgressBar) findViewById(R.id.progress_bar); } public void onclick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择您喜欢的课程"); final String[] items = new String[]{"Android", "ios", "c", "c++", "html", "c#"}; // -1表明没有条目被选中 builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 把选项取出来 String item = items[which]; Toast.makeText(MainActivity.this, item, Toast.LENGTH_SHORT).show(); // 把对话框关闭 dialog.dismiss(); } }); builder.show(); } public void onclick1(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择您喜欢吃的水果"); final String[] items = new String[]{"香蕉", "黄瓜", "哈密瓜", "西瓜", "梨", "柚子", "榴莲"}; // 定义对应的选中数组,默认所有未选中 final boolean[] checkedItems = {false, false, false, false, false, false, false}; builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { } }); builder.setPositiveButton("肯定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 把选中的显示出来 StringBuilder sb = new StringBuilder(); for (int i = 0; i < items.length; ++i) { if (checkedItems[i]) { sb.append(items[i] + " "); } } Toast.makeText(MainActivity.this, sb, Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.show(); } public void onclick2(View view) { progressBar.setVisibility(View.VISIBLE); Log.d(TAG, "onclick2: "); new Thread() { @Override public void run() { for (int i = 0; i <= 100; ++i) { progressBar.setProgress(i); SystemClock.sleep(100); } runOnUiThread(new Runnable() { @Override public void run() { progressBar.setVisibility(View.GONE); // 操做界面必须在主线程,这句话不在主线程就崩了。 } }); } }.start(); } }
批注:若是须要让其强制不能取消,点击对话框以外的地方也不会返回,只能选中后点肯定。
设置属性builder.setCancelable(false);便可
activity_main.xml
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick" android:text="单选对话框" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onclick1" android:text="多选对话框" /> </LinearLayout>
运行效果图:
官方文档地址:https://developer.android.google.cn/guide/topics/graphics/drawable-animation
效果就是把一些图片连续播放造成动画效果,
在drawable文件夹下new-Drawable resource file
根元素设置为animation-list
my_anim.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <!--oneshot的true表明是执行一次,false表明动画反复执行--> <item android:drawable="@drawable/girl_1" android:duration="200" /> <item android:drawable="@drawable/girl_2" android:duration="200" /> <item android:drawable="@drawable/girl_3" android:duration="200" /> <item android:drawable="@drawable/girl_4" android:duration="200" /> <item android:drawable="@drawable/girl_5" android:duration="200" /> <item android:drawable="@drawable/girl_6" android:duration="200" /> <item android:drawable="@drawable/girl_7" android:duration="200" /> <item android:drawable="@drawable/girl_8" android:duration="200" /> <item android:drawable="@drawable/girl_9" android:duration="200" /> <item android:drawable="@drawable/girl_10" android:duration="200" /> <item android:drawable="@drawable/girl_11" android:duration="200" /> </animation-list>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
MainActivity.java
import android.graphics.drawable.AnimationDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到控件显示动画 ImageView rocketImage = (ImageView) findViewById(R.id.iv); // 设置背景资源 rocketImage.setBackgroundResource(R.drawable.my_anim); // 获取AnimationDrawable类型 AnimationDrawable rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); // 开启动画 rocketAnimation.start(); } }
运行效果图:
===========================Talk is cheap, show me the code======================