open coding

open coding - Recourse(String.xml, String_array.xml..etc.)

◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e 2020. 11. 26. 14:14
반응형

srings.xml, string-array.xlm, colors.xml, dimes.xml

<MainActivity>

package kr.co.softcampus.resource;

import android.content.res.Resources;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView text1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text1 = (TextView)findViewById(R.id.textView);
    }

    public void btn1Method(View view){
//        Resources res = getResources();
//        String str1 = res.getString(R.string.str2);
//        text1.setText(str1);
        text1.setText(R.string.str2);       //text는 걍 바로 R.string.str2해도 적용된다
                                            // 다른건 getResources.getString(R.string.str2)식으로 해야 오류 없다.
    }

    public void btn2Method(View view){
        Resources res = getResources();
        String [] array = res.getStringArray(R.array.data_array);   //String array

        text1.setText("");
        for(String str1 : array){
            text1.append(str1 + "\n");
        }
    }

    public void btn3Method(View view){
        // text1.setTextColor(Color.YELLOW);
        // int color = Color.rgb(26, 106, 129);         //좀더 자유롭게 칼라설정
        // int color = Color.argb(50, 26, 106, 129);    // 투명도 설정.
        int color = ContextCompat.getColor(this, R.color.color4);       //color.xml 사용.
        text1.setTextColor(color);
    }

    public void btn4Method(View view){
        Resources res = getResources();
                                                    //각각 px로 환산한 값이 나온다.
                                                    // 자바는 전부 px단위이다.
        float px = res.getDimension(R.dimen.px);
        float dp = res.getDimension(R.dimen.dp);
        float sp = res.getDimension(R.dimen.sp);
        float inch = res.getDimension(R.dimen.inch);
        float mm = res.getDimension(R.dimen.mm);
        float pt = res.getDimension(R.dimen.pt);

        text1.setText("px : " + px + "\n");
        text1.append("dp : " + dp + "\n");
        text1.append("sp : " + sp + "\n");
        text1.append("inch : " + inch + "\n");
        text1.append("mm : " + mm + "\n");
        text1.append("pt : " + pt + "\n");

        text1.setTextSize(20 * dp);     // 텍스트 사이즈



    }
}

각각  xml


//Strings.xml ----------------------------------------------------

<resources>
    <string name="app_name">Resource</string>
    <string name="str1">안녕하세요</string>
    <string name="str2">반갑습니다</string>
</resources>



//String_array.xml-------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="data_array">
        <item>항목1</item>
        <item>항목2</item>
        <item>항목3</item>
        <item>항목4</item>
        <item>항목5</item>
        <item>항목6</item>
    </string-array>
</resources>


//colors.xml----------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>

    <color name="color1">#F00</color>
    <color name="color2">#5F00</color>
    <color name="color3">#00FF00</color>
    <color name="color4">#5500FF00</color>
</resources>

<!--


    #F00

    #5F00 투명도 들어간거.

    #00FF00(레드/그린/블루)-->
    
    

//dimen.xml-------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="px">1px</dimen>
    <dimen name="inch">1in</dimen>
    <dimen name="mm">1mm</dimen>
    <dimen name="pt">1pt</dimen>
    <dimen name="dp">1dp</dimen>
    <dimen name="sp">1sp</dimen>
</resources>