상세 컨텐츠

본문 제목

[안드로이드] 안드로이드 스튜디오 기본 색상 코드에서 가져오는 방법

안드로이드

by aries574 2021. 1. 23. 13:36

본문



이번 시간에는 안드로이드 스튜디오에서 기본으로 지원하는 색상을 코드에서 가져오는 방법을 알아보겠습니다.


1. 메인화면 구현(activity_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:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">


<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/black" />

<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/black" />

<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/black" />

<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView10"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/white" />

<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:textColor="@android:color/black" />


</LinearLayout>


2. 메인코드 구현(MainActivity.java)


import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity{

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

TextView textView1 = findViewById(R.id.textView1);
TextView textView2 = findViewById(R.id.textView2);
TextView textView3 = findViewById(R.id.textView3);
TextView textView4 = findViewById(R.id.textView4);
TextView textView5 = findViewById(R.id.textView5);
TextView textView6 = findViewById(R.id.textView6);
TextView textView7 = findViewById(R.id.textView7);
TextView textView8 = findViewById(R.id.textView8);
TextView textView9 = findViewById(R.id.textView9);
TextView textView10 = findViewById(R.id.textView10);
TextView textView11 = findViewById(R.id.textView11);

textView1.setBackgroundColor(Color.RED);
textView2.setBackgroundColor(Color.BLUE);
textView3.setBackgroundColor(Color.GREEN);
textView4.setBackgroundColor(Color.YELLOW);
textView5.setBackgroundColor(Color.GRAY);
textView6.setBackgroundColor(Color.CYAN);
textView7.setBackgroundColor(Color.DKGRAY);
textView8.setBackgroundColor(Color.LTGRAY);
textView9.setBackgroundColor(Color.MAGENTA);
textView10.setBackgroundColor(Color.BLACK);
textView11.setBackgroundColor(Color.WHITE);

textView1.setText("RED");
textView2.setText("BLUE");
textView3.setText("GREEN");
textView4.setText("YELLOW");
textView5.setText("GRAY");
textView6.setText("CYAN");
textView7.setText("DKGRAY");
textView8.setText("LTGRAY");
textView9.setText("MAGENTA");
textView10.setText("BLACK");
textView11.setText("WHITE");
}
}


3. 실행화면






공감 댓글 환영합니다.



반응형

관련글 더보기

댓글 영역