|
안드로이드 개발 RelativeLayout 화면을 Java 소스에서 구현하는 방법 |
|
환경: Android Studio |
기본적으로 디자인과 코드를 분리하는 것이 좋기 때문에 안드로이드 화면을 구성하는 소스는 XML 로 작성합니다. 하지만 모든 화면 레이아웃은 Java 코드로 구현이 가능합니다. 경우에 따라서 동적으로 화면을 구성해야 되기 때문입니다. 이런 예외적인 경우가 아니라면 디자인과 로직을 구현한 소스는 분리하는 것이 좋습니다.
▼ 먼저 XML 로 어떤 형태인지 세팅해 보았습니다. 화면에서 부모뷰를 기준으로 3가지 형태의 버튼을 배열합니다.
▼ 위의 화면을 구성한 XML 소스는 다음과 같습니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="display.samsung.workplace.RelativeLayoutctivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="왼쪽상단" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="화면중앙" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="오른쪽하단" />
</RelativeLayout>
▼ 이제 XML 소스를 사용하지 않고 Java 코드에서 구현해 보겠습니다. 기존에 onCreate 함수에서 XML 을 이용한 화면 생성 함수인 setContentView 는 주석처리 합니다. 그리고 부모뷰인 RelativeLayout 객체를 생성합니다.
public class RelativeLayoutctivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Java 코드로 구현하기 위해 화면 구성 함수는 주석처리 합니다.
//setContentView(R.layout.activity_relative_layoutctivity);
// RelativeLayout 객체를 생성합니다.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
rl.setLayoutParams(params);
}
}
▼ 다음은 버튼을 하나씩 만들어서 추가해 보겠습니다. 버튼 위젯을 만드는 과정은 RelativeLayout 때와 동일합니다. 추가된 내용은 RelativeLayout 에서 버튼을 부모뷰에 배치하는 옵션입니다. LayoutParams 의 addRule() 함수를 이용해서 첫 번째 버튼의 위치를 지정했습니다.
// 첫 번재 오른쪽 상단 버튼 구현
Button topButton = new Button(this);
topButton.setId(View.generateViewId());
topButton.setText("왼쪽상단");
RelativeLayout.LayoutParams topButtonParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
topButton.setLayoutParams(topButtonParams);
topButtonParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
topButtonParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
rl.addView(topButton);
setContentView(rl);
▼ 나머지 두 개의 버튼을 추가한 전체 소스 입니다. addRule() 함수를 이용해서
버튼을 배치했습니다. 중앙에 배치하는 룰은 RelativeLayout.CENTER_IN_PARENT
입니다. 마지막에 RelativeLayout 에
만든 버튼을 추가하는 함수는 addView() 입니다. 그리고
setContentView() 함수에 XML Resource ID 가
아닌 RelativeLayout 객체를 입력합니다.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class RelativeLayoutctivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Java 코드로 구현하기 위해 화면 구성 함수는 주석처리 합니다.
//setContentView(R.layout.activity_relative_layoutctivity);
// RelativeLayout 객체를 생성합니다.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
rl.setLayoutParams(params);
// 첫 번재 오른쪽 상단 버튼 구현
Button topButton = new Button(this);
topButton.setId(View.generateViewId());
topButton.setText("왼쪽상단");
RelativeLayout.LayoutParams topButtonParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
topButton.setLayoutParams(topButtonParams);
topButtonParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
topButtonParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
// 두 번째 화면 중앙 버튼 구현
Button middleButton = new Button(this);
middleButton.setId(View.generateViewId());
middleButton.setText("화면중앙");
RelativeLayout.LayoutParams middleButtonParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
middleButton.setLayoutParams(middleButtonParams);
middleButtonParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
// 세 번째 화면 오른쪽 하단 버튼 구현
Button bottomButton = new Button(this);
bottomButton.setId(View.generateViewId());
bottomButton.setText("오른쪽하단");
RelativeLayout.LayoutParams bottomButtonParams = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
bottomButton.setLayoutParams(bottomButtonParams);
bottomButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bottomButtonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// RelativeLayout 에 모두 추가한다.
rl.addView(topButton);
rl.addView(middleButton);
rl.addView(bottomButton);
setContentView(rl);
}
}
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드 개발 setId() 에러 Expected resource of type id 해결하는 방법 (0) | 2018.01.02 |
|---|---|
| 안드로이드 개발 Activity 호출하고 결과값을 받는 onActivityForResult 사용하는 방법 (0) | 2017.12.30 |
| 안드로이드 개발 서비스(Service) 컴포넌트 예제 구현하는 방법 (0) | 2017.12.27 |
| 안드로이드 개발 생성자 추가하는 방법, There is no default constructor available in (0) | 2017.12.23 |
| 안드로이드 개발 No orientation specified, and the default is horizontal. This is a common source of bugs when children are added dynamically. 에러 해결 (0) | 2017.12.06 |
| 안드로이드 개발 Android WebView 로컬 HTML 파일 표현하는 방법 (0) | 2017.12.04 |
| 안드로이드(Android) 개발 컴포넌트 공유 객체 Application 만들어서 사용하는 방법 (0) | 2017.11.30 |
| 안드로이드 개발 가로/세로 화면 전환할 때 배경화면 변경하는 방법 (0) | 2017.11.27 |

