目录

样式:

主题:

国际化(本地化):

对话框:

帧动画:


 

样式:

没用样式之前,修改特别麻烦,一旦需求改变,比如TextView颜色不对,字体大小不对,都需要一个个修改TextView

使用样式的好处就是将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添加这样一段:

    <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> 

这样就变成了如下效果:

 

 

主题:

设置样式的方法有两种:

  • 如果是对单个视图应用样式,请为布局 XML 中的 View 元素添加 style 属性。
  • 或者,如果是对整个 Activity 或应用来应用样式,请为 Android 清单中的 <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======================