Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> android 資源和國際化(續二)

android 資源和國際化(續二)

編輯:初級開發

自定義View,eg:API LabelVIEw.Java
在res/values/
               |_arrays.XML
               |_attrs.XML
               |_colors.XML 關於該目錄
(一),可以被編譯成很多中類型的資源XML文件
(二),不象其他的/res/folder,他可以保存任意數量的文件,這些文件保存了要創建資源的描述,而不是資源本身,XML元素類型控制這些資源應該放在R類的什麼地方。 arrars.XML 定義數據
colors.XML 定義 color drawable 和顏色的字符串值(color string values).使用Resource.getDrawable()和Resource.getColor()分別獲得這些資源。
dimens.XML 定義尺寸值(dimension value),使用Resource.getDimension()獲得這些資源值
strings.XML   
styles.XML 定義樣式(style)對象 如下API中定一的一些文件
arrars.XML 
<?XML version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The android Open Source Project      Licensed under the apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at           http://www.apache.org/licenses/LICENSE-2.0      Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implIEd.
     See the License for the specific language governing permissions and
     limitations under the License.
--> <resources>
    <!-- Used in VIEw/Spinner1.Java -->
    <string-array name="colors">
        <item>red</item>
        <item>orange</item>
        <item>yellow</item>
        <item>green</item>
        <item>blue</item>
        <item>violet</item>
    </string-array>
        <!-- Used in VIEw/Spinner1.Java -->
    <string-array name="planets">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
        <item>Pluto</item>
    </string-array>     <!-- Used in App/SearchInvoke.Java -->
    <string-array name="search_menuModes">
        <item>Search Key</item>
        <item>Menu Item</item>
        <item>Type-To-Search</item>
        <item>Disabled</item>
    </string-array>     <!-- Used in app/dialog examples -->
    <string-array name="select_dialog_items">
        <item>Command one</item>
        <item>Command two</item>
        <item>Command three</item>
        <item>Command four</item>
    </string-array>     <string-array name="select_dialog_items2">
        <item>Map</item>
        <item>Satellite</item>
        <item>Traffic</item>
        <item>Street vIEw</item>
    </string-array>     <string-array name="select_dialog_items3">
        <item>Every Monday</item>
        <item>Every Tuesday</item>
        <item>Every Wednesday</item>
        <item>Every Thursday</item>
        <item>Every Friday</item>
        <item>Every Saturday</item>
        <item>Every Sunday</item>
    </string-array>     <!-- Used in app/menu examples -->
    <string-array name="entrIEs_list_preference">
        <item>Alpha Option 01</item>
        <item>Beta Option 02</item>
        <item>CharlIE Option 03</item> 
    </string-array>     <!-- Used in app/menu examples -->
    <string-array name="entryvalues_list_preference">
        <item>alpha</item>
        <item>beta</item>
        <item>charlIE</item> 
    </string-array> </resources> attrs.XML
<?XML version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="LabelVIEw">
        <attr name="text" format="string" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
    </declare-styleable>
    <declare-styleable name="TestVIEw">
        <attr name="text1" format="string" />
        <attr name="textColor1" format="color" />
        <attr name="textSize1" format="dimension" />
    </declare-styleable>
</resources> colors.XML
<?XML version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The android Open Source Project      Licensed under the apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at           http://www.apache.org/licenses/LICENSE-2.0      Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implIEd.
     See the License for the specific language governing permissions and
     limitations under the License.
--> <resources>
    <drawable name="red">#7f00</drawable>
    <drawable name="blue">#770000ff</drawable>
    <drawable name="green">#7700ff00</drawable>
    <drawable name="yellow">#77ffff00</drawable>     <drawable name="screen_background_black">#ff000000</drawable>
    <drawable name="translucent_background">#e0000000</drawable>
    <drawable name="transparent_background">#00000000</drawable>     <color name="solid_red">#f00</color>
    <color name="solid_blue">#0000ff</color>
    <color name="solid_green">#f0f0</color>
    <color name="solid_yellow">#ffffff00</color> </resources>
LableVIEw.Java
// Need the following import to get Access to the app resources, since this
// class is in a sub-package.
import com.spt.test.R; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.VIEw; /**
* Example of how to write a custom subclass of View. LabelVIEw
* is used to draw simple text vIEws. Note that it does not handle
* styled text or right-to-left writing systems. public class LabelView extends VIEw {
    private Paint mTextPaint;
    private String mText;
    private int mAscent;     /**
     * Constructor. This version is only needed if you will be instantiating
     * the object manually (not from a layout XML file).
     * @param context     public LabelVIEw(Context context) {
        super(context);
        initLabelVIEw();      * Construct object, initializing with any attributes we understand from a
     * layout file. These attributes are defined in
     * SDK/assets/res/any/classes.XML.      * @see android.view.View#VIEw(android.content.Context, android.util.AttributeSet)     public LabelVIEw(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLabelVIEw();
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.LabelView);         CharSequence s = a.getString(R.styleable.LabelVIEw_text);
        if (s != null) {
            setText(s.toString());
        }         // Retrieve the color(s) to be used for this vIEw and apply them.
        // Note, if you only care about supporting a single color, that you
        // can instead call a.getColor() and pass that to setTextColor().
        setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));         int textSize = a.getDimensionPixelOffset(R.styleable.LabelVIEw_textSize, 0);
        if (textSize > 0) {
            setTextSize(textSize);
        }         a.recycle();
    }     private final void initLabelVIEw() {
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(16);
        mTextPaint.setColor(0xFF000000);
        setPadding(3, 3, 3, 3);      * Sets the text to display in this label
     * @param text The text to display. This will be drawn as one line.     public void setText(String text) {
        mText = text;
        requestLayout();
        invalidate();      * Sets the text size for this label
     * @param size Font size     public void setTextSize(int size) {
        mTextPaint.setTextSize(size);
        requestLayout();
        invalidate();      * Sets the text color for this label.
     * @param color ARGB value for the text     public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();      * @see android.view.VIEw#measure(int, int)     @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));      * Determines the width of this vIEw
     * @param measureSpec A measureSpec packed into an int
     * @return The width of the vIEw, honoring constraints from measureSpec     private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);         if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text
            result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);         }         return result;      * Determines the height of this vIEw
     * @param measureSpec A measureSpec packed into an int
     * @return The height of the vIEw, honoring constraints from measureSpec     private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);         mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            // We were told how big to be
            result = specSize;
        } else {
            // Measure the text (beware: ascent is a negative number)
            result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                // Respect AT_MOST value if that was what is called for by measureSpec
                result = Math.min(result, specSize);         }
        return result;      * Render the text      * @see android.view.VIEw#onDraw(android.graphics.Canvas)     @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint); }
custom_vIEw_1.XML
<?XML version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The android Open Source Project      Licensed under the apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at           http://www.apache.org/licenses/LICENSE-2.0      Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implIEd.
     See the License for the specific language governing permissions and
     limitations under the License.
--> <!-- Demonstrates defining custom vIEws in a layout file. --> <LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
        XMLns:app="http://schemas.android.com/apk/res/custom_vIEw_dir"
        android:orIEntation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">     <custom_vIEw_dir
            android:background="@drawable/red"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            app:text="Red"/>     <custom_vIEw_dir
            android:background="@drawable/blue"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            app:text="Blue" app:textSize="20dp"/>     <custom_vIEw_dir
            android:background="@drawable/green"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            app:text="Green" app:textColor="#ffffffff" /> </LinearLayout>
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved