안드로이드 XMl

안드로이드에서 xml은 레이아웃을 담당한다. 그리고 xml 안에 xml을 넣는 경우가 있다.


예를 들어 버튼이나 텍스트뷰의 모서리 모양을 둥글게 하고 싶을 때, drawable에 둥근 모양을 설정하는 xml파일을 넣는다.


그리고 레이아웃을 구성하는 xml에서 파일을 불러와 둥근 모서리를 구현한다. 


안드로이드 자체 xml 에서는 둥근 모양을 설정할 수 없었기 때문에 이런 비효율적인 방법으로 구현할 수 밖에 없었다.




그리고 오늘 나의 경우는 둥근 모서리의 레이아웃을 구현하되, 특정 레이아웃을 클릭하면 둥근 모서리의 레이아웃의 색상도 변경해야 했다.




firstboard.xml


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#FFFFFF"/>

<!-- 테두리
<stroke android:width="10dip" android:color="#5DA4BE" />
-->
<corners android:bottomLeftRadius="20dip"
android:bottomRightRadius="20dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>



secondboard.xml


<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#5DA4BE"/>

<!-- 테두리
<stroke android:width="10dip" android:color="#5DA4BE" />
-->
<corners android:bottomLeftRadius="20dip"
android:bottomRightRadius="20dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>




activity_main.xml 중 일부분


<LinearLayout
android:id="@+id/bottomboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8"
android:background="@drawable/firstboard"
android:orientation="vertical">
</LinearLayout>



HomeActivity.java


findViewById(R.id.firstlayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

findViewById(R.id.bottomboard).setBackground(
getResources().getDrawable(R.drawable.firstboard)
);
}
});



레이아웃 클릭 시, 색상변경

+ Recent posts