顯示具有 xml 標籤的文章。 顯示所有文章
顯示具有 xml 標籤的文章。 顯示所有文章

2017年3月29日 星期三

Android how to parse the XML

XML Content


/*
<car>
<vacancyinfo capacity="327" vacancy="145"/>
</car>*/



parse Program

xml = connection(SharedUtils.CAR_VACANCY_URL, null);

InputStream inputStream = new    ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList vacancyinfoNodeLis = doc.getElementsByTagName("vacancyinfo");
Element vacancyinfo = (Element) vacancyinfoNodeLis.item(0);
String capacity=vacancyinfo.getAttribute("capacity");
String vacancy=vacancyinfo.getAttribute("vacancy");

2015年12月28日 星期一

Android 使用xliff 格式化字符串

Copy from http://ezfantasy.iteye.com/blog/1489273

Android 使用xliff 格式化字符串



Android資源字符串/res/values​​/string.xml中可以包含xliff的節點,Xliff是XML Localization Interchange File Format 的縮寫,中文名為XML本地化數據交換格式。

quote from wikipedia ( http://en.wikipedia.org/wiki/XLIFF ) :
"XLIFF  ( XML Localisation Interchange File Format ) is an  XML -based format created to standardize localization . XLIFF was standardized by  OASIS  in 2002. Its current specification is v1.2 [1]  released on Feb-1-2008.
The specification is aimed at the localization industry. It specifies elements and attributes to aid in localization.
XLIFF forms part of the Open Architecture for XML Authoring and Localization ( OAXAL ) reference architecture."

<xliff:g>標籤介紹:
屬性id可以隨便命名
屬性值舉例說明
%n$ms:代表輸出的是字符串,n代表是第幾個參數,設置m的值可以在輸出之前放置空格
%n$ md:代表輸出的是整數,n代表是第幾個參數,設置m的值可以在輸出之前放置空格,也可以設為0m,在輸出之前放置m個0
%n$mf:代表輸出的是浮點數,n代表是第幾個參數,設置m的值可以控制小數位數,如m=2.2時,輸出格式為00.00 

也可簡單寫成:
%d (表示整數)
%f (​​表示浮點數)
%s (表示字符串)

使用步驟舉例:
1.
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
2.
 <string name="test_xliff">小紅今年<xliff:g id="xxx">%d</xliff:g>歲了,上<xliff:g id="yyy">%s</xliff:g >年級!</string>
3. 
String test = String.format(getResources().getString(R.string.test_xliff), 7, "小學二");

輸出:
小紅今年7歲了,上小學二年級!

2015年11月3日 星期二

Menu Button

1.Add at the menu/main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="10"
        android:title="@string/action_refresh"
        app:showAsAction="ifRoom" />
</menu>

2A  in Activity show the menu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


2B in Fragment show the menu

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.forecastfragment,menu);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_refresh:

                break;
        }
        return super.onOptionsItemSelected(item);
    }



2015年11月2日 星期一

Style

If many view use the same attribute,we can change it to astyle


<Button
        android:id="@+id/btBTB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/orange_custom_button_background"
        android:padding="5dp"
        android:text="@string/ant_terminator"
        android:textColor="@color/textColor" />




change to a style


in styles.xml


<style name="OrangeButton">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_margin">10dp</item>
        <item name="android:background">@drawable/orange_custom_button_background</item>
        <item name="android:padding">5dp</item>
        <item name="android:textColor">@color/textColor</item>
    </style>


After apply





<Button
        android:id="@+id/btBTB"
        style="@style/OrangeButton"
        android:text="@string/ant_terminator" />

2015年10月29日 星期四

Change the button background

1.at drawable create a xml file.eg custom_button_background.xml

2.define the color in the values/colors.xml

3.in custom_button_background.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/orangeDark" android:state_pressed="true"></item>
    <item android:drawable="@color/orange"></item>

</selector>


android:state_pressed="true" define when press, the color change to this color.

4.Use


<Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/custom_button_background"
        android:padding="5dp"
        android:text="@string/materiallize"
        android:textColor="@color/textColor" />


different state:

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList


2015年10月18日 星期日

String Resource

In android, the string can as a Resource, define in the xml.
1.define the String, in strings.xml

<resources>
    <string name="app_name">L013String</string>
    <string name="word">Word</string>
</resources>


2.Use in other XML,e.g.,activity_main.xml


<TextView android:text="@string/word" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


3. Use in the program java code.





 String w=getResources().getString(R.string.word);

2015年10月14日 星期三

制作自定義的View4-使用XML修改 View




package pom.poly.com.trymakeviewbymyself.myView.v4;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;

import pom.poly.com.trymakeviewbymyself.R;
import pom.poly.com.trymakeviewbymyself.myView.v3.BasicView;

/**
 * Created by User on 14/10/2015.
 */
public class NumView extends BasicView {
    private Paint paint = new Paint();
    private String showText = "I love you";
    private int linenum=4;

    public NumView(Context context) {
        super(context);
    }

    public NumView(Context context, AttributeSet attrs) {
        super(context, attrs);
       
    }

    @Override
    protected void drawsub(Canvas canvas) {
        int textSize=100;
        for (int i = 0; i < linenum; i++) {
            textSize = textSize + i*10;
            paint.setTextSize(textSize);
            canvas.drawText(showText, 0, textSize + textSize*i, paint);
        }


    }

    @Override
    protected void logic() {

    }
}

How to no need change the code, use XML to change the number of line that show?
1.create a attrs.xml

in the xmal file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="NumView">
        <attr name="linenum" format="integer"></attr>
    </declare-styleable>
</resources>

remark:<attr name="linenum" format="integer"></attr>  定義了 linenum e個 attribute 的鄰形是 int

2. use the XML
in the activity_mian.xml
add

xmlns:nv="http://schemas.android.com/apk/res-auto"

than you can use the linenum

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:nv="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <pom.poly.com.trymakeviewbymyself.myView.v4.NumView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:id="@+id/view"
        android:layout_weight="1"
        nv:linenum="6"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/bt1" />
</LinearLayout>

3.change the program that make the program can read the value type in the XML

public NumView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.NumView);
        linenum=ta.getInt(R.styleable.NumView_linenum,0);
        ta.recycle();//release the TypedArray
    }

remark:NumView define in the attrs.xml ,
must need release the TypedArray