android學習筆記----樣式、主題、國際化(本地化)、對話框、幀動畫

 

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

這樣就變成了以下效果:

 

 

主題:

設置樣式的方法有兩種:

  • 若是是對單個視圖應用樣式,請爲佈局 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======================

相關文章
相關標籤/搜索