[안드로이드/코틀린] resource

박의진·2022년 9월 7일
0

1.drawable 리소스

.png 같은 이미지 뿐만 아닐 xml로도 기본 이미지를 만들어 줄 수 있다.

* <shape> 도형을 의미
•android:shape="rectangle" 처럼 shape 속성을 이용하여 도형의 타입을 지정
•지정 가능한 값은 rectangle, oval, line, ring 중 하나
  
* <corners> - 둥근 모서리를 만들기 위해 사용. shape 값이 rectangle 인 경우에만 적용됨
* <gradient> - 그라데이션 색상을 지정하기 위해 사용
* <size> - 도형의 크기
* <solid> - 도형의 색상
* <stroke> - 도형에 대한 스트로크 선

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding = "10dp">
    <solid android:color="#6666FF"></solid>
    <corners
        android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp"></corners>
</shape>

my_image.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient **그라데이션효과 속성
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <corners android:radius="8dp" />
</shape>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@drawable/my_image"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="@drawable/round_button"
        android:textColor="#FFFFFF"
        android:textStyle="bold"
        android:layout_marginTop="24dp"/>
</LinearLayout>

실행결과

2.values 리소스

•값으로 이용되는 리소스를 저장하기 위한 폴더
•문자열(string), 색상(color), 사이즈(dimen), 스타일(style), 배열
•리소스 파일명으로 식별되지 않고 리소스 태그의 name 값으로 식별

1. colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="txt_color">#FFFF00</color>
    <color name="txt_bg_color">#FF0000</color>
</resources>

2. dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="txt_size">25dp</dimen>
</resources>

3. styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTextStyle">
        <item name="android:textSize">@dimen/txt_size</item>
        <item name="android:textColor">@color/txt_color</item>
    </style>

**parent를 사용하여 상속받음.일종의 상속!!
    <style name="MyTextStyleSub" parent="MyTextStyle">
        <item name="android:textColor">#0000FF</item>
        <item name="android:background">@color/txt_bg_color</item>
    </style>
</resources>

4. main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txt_data1"
        android:textColor="@color/txt_color"
        android:background="@color/txt_bg_color"
        android:textSize="@dimen/txt_size"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/MyTextStyleSub"
        android:text="Hello World"
        android:layout_marginTop="24dp"/>
</LinearLayout>

5. MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textView = findViewById<TextView>(R.id.textView2)
        textView.text=getString(R.string.txt_data2)
        textView.setTextColor(ResourcesCompat.getColor(resources, R.color.txt_color, null))
        textView.setTextSize(resources.getDimension(R.dimen.txt_size))
    }
}

6. 실행결과

3.리소스 조건 명시

디바이스마다 dpi가 다르기 때문에 이걸 폴더 명에 명시 한다.

1. 리소스 조건

•이동통신 국가 코드 - mcc310, mcc310-mnc004
•언어 및 지역 - en, ko-rKR
•레이아웃 방향 - ldrtl, ldltr
•이용 가능한 너비 - w720dp
•이용 가능한 높이 - h720dp
•화면크기 - small, normal, large, xlarge
•화면비율 - long, notlong
•원형 화면 - round, notround
•화면방향 - port, land
•UI 모드 - car, desk, television, application, watch, vrheadset •야간 모드 - night, notnight •화면 픽셀밀도 - ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, nodpi, tvdpi, anydpi, nnndpi •터치스크린유형 - notouch, finger
•키보드가용성 - Keysexposed, keyshidden, keyssoft •기본 텍스트 입력방법 - nokeys, qwerty, 12key
•탐색키가용성 - navexposed, navhidden
•기본 비터치 탐색방법 - nanav, dpad, trackball, wheel •플랫폼 버전 - v21

리소스 조건 명시로 예를들어 국가별 언어나 디바이스별 dpi 를 설정해 줄 수 있다.

2. 국가별 string.xml 설정

영어버전


<resources>
    <string name="app_name">C29</string>
    <string name="data">Hello</string>
</resources>

ko버전

<resources>
    <string name="app_name">C29</string>
    <string name="data">안녕</string>
</resources>

3. 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="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/data"
        android:layout_margin="16dp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="@string/data"
        android:layout_margin="16dp"/>

</LinearLayout>

land 버전

<?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="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/data"
        android:layout_margin="16dp"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/data"
        android:layout_margin="16dp"/>
</LinearLayout>

4. 실행결과

profile
주니어 개발자의 개발일지

0개의 댓글