동적레이아웃(DynamicLayout) - width, height

Posted by ITPangPang
2016.10.09 18:10 안드로이드(android)/레이아웃(Layout)



동적레이아웃(DynamicLayout)

width, height



ㆍ LayoutParams


ㆍ setLayoutParams


ㆍ MATCH_PARENT


ㆍ WRAP_CONTENT




이번글에서 첫번째

만들어볼 화면 4가지



첫번째 화면

width = WRAP_CONTENT

height = WRAP_CONTENT


두번째 화면

width = MATCH_PARENT

height = WRAP_CONTENT


세번째 화면

width = WRAP_CONTENT

height = MATCH_PARENT


네번째 화면

width = MATCH_PARENT

height = MATCH_PARENT



코드작성

1. 기본적인 전체 레이아웃 LinearLayout 생성

2. LinearLayout에 들어갈 Button 생성

3. Button에 HELLO WORLD! 텍스트 넣기

4. LinearLayout의 width, height값을 MATCH_PARENT 적용

5. Button의 width, height을 변경해보면서 4가지 화면 구현



1. 기본적인 전체 레이아웃 LinearLayout 생성


public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

LinearLayout ll = new LinearLayout(this);
setContentView(ll);

}
}


2. LinearLayout에 들어갈 Button 생성


public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

LinearLayout ll = new LinearLayout(this);
Button btn = new Button(this);

ll.addView(btn);
setContentView(ll);
}
}



3. Button에 HELLO WORLD! 텍스트 넣기


public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

LinearLayout ll = new LinearLayout(this);
Button btn = new Button(this);
btn.setText("HELLO WORLD!");

ll.addView(btn);
setContentView(ll);
}
}



4. LinearLayout의 width, height값을 MATCH_PARENT 적용


LayoutParams

: 레이아웃을 동적으로 생성할때

width, height, margin, padding등의

초기값을 설정해 줄 수 있다


setLayoutParams

: 설정한 초기값을 해당 View에

적용시킬 수 있다.


public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(param);


Button btn = new Button(this);
btn.setText("HELLO WORLD!");

ll.addView(btn);
setContentView(ll);
}
}


5. Button의 width, height을 변경해보면서 4가지 화면 구현


첫번째 화면

width = WRAP_CONTENT

height = WRAP_CONTENT

public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(param);

Button btn = new Button(this);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(lp);

btn.setText("HELLO WORLD!");

ll.addView(btn);
setContentView(ll);
}
}



두번째 화면

width = MATCH_PARENT

height = WRAP_CONTENT


(공통코드 생략)

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);



세번째 화면

width = MATCH_PARENT

height = WRAP_CONTENT

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);




네번째 화면

width = MATCH_PARENT

height = MATCH_PARENT

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);






이번글에서

번째

만들어볼 화면




버튼의 크기를

정해서 생성하기

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(원하는 width, 원하는 height);


public class MainActivity extends AppCompatActivity
{
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(param);

btn = new Button(this);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(200,200);

btn.setLayoutParams(lp);
btn.setText("HELLO WORLD!");

ll.addView(btn);
setContentView(ll);
}
}


Toast를 찍기 위해서는?


Toast.makeText(this,"width : "+btn.getWidth()+"\n"+"height : "+btn.getHeight(),Toast.LENGTH_LONG).show();



어디다가 작성?

setContentView(ll);

아래?


NO NO!!


왜 아래에 쓰면 안되는가?

아주 과거에 썼던글

http://itpangpang.xyz/128



최종코드

public class MainActivity extends AppCompatActivity
{
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);

LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(param);

btn = new Button(this);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(200,200);

btn.setLayoutParams(lp);
btn.setText("HELLO WORLD!");

ll.addView(btn);
setContentView(ll);
}
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
Toast.makeText(this,"width : "+btn.getWidth()+"\n"+"height : "+btn.getHeight(),Toast.LENGTH_LONG).show();
super.onWindowFocusChanged(hasFocus);
}

}


저작자 표시 비영리 변경 금지
신고
이 댓글을 비밀 댓글로