본문 바로가기
MOBILE/Android

Code Layout 방식과 XML Layout 방식

by 지구 2018. 5. 29.

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world!"/>
 
</android.support.constraint.ConstraintLayout>
cs



MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        //XML layout 방식
        setContentView(R.layout.activity_main);
 
        //code layout 방식
        LinearLayout linearLayout = new LinearLayout(this);
 
        TextView textView = new TextView(this);
        String message = "안녕, Hello Android! Hard Coding";
        textView.setText(message);
 
        linearLayout.addView(textView);
 
        setContentView(linearLayout);
    }
}
cs



실제 보여지는 결과물은 동일하나 유지관리 측면에서는 XML Layout 방식이 좋다고 한다.

하지만, 동적인 화면을 구성할 땐 Code Layout 방식이 더 편함.

반응형

댓글