동적레이아웃(DynamicLayout) - Margin, Padding

Posted by ITPangPang
2016.10.10 22:55 안드로이드(android)/레이아웃(Layout)



동적레이아웃(DynamicLayout)

Margin, Padding



ㆍ setPadding


ㆍ setMargins


ㆍ leftMargin, rightMargin, topMargin, bottomMargin



간단하게

 Padding과 Margin 알아보기






Default Padding


Padding 값 증가



가운데 o버튼 기준

Default Margin


Margin 값 증가


setPadding(left, top, right, bottom)


그냥 setPadding 하고

주면 끝난다


첫번째,

left, top Padding : 30

right, bottom Padding : 0

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");
btn.setPadding(30, 30, 0, 0);

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


결과화면



두번째,

전체 Padding : 50

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");
btn.setPadding(50, 50, 50, 50);

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


결과화면



setMargins(left, top, right, bottom)


LayoutParams를 추가해서

setMargins 값을 설정해 준후에

button에 적용시킨다



첫번째,

top Margin : 100

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");

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,100,0,0);
btn.setLayoutParams(lp);


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


결과화면



두번째,

top Margin : 100

left Margin : 200

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");

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(200,100,0,0);
btn.setLayoutParams(lp);

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


결과화면


leftMargin, rightMargin, topMargin, bottomMargin


따로 주는 것도

가능하다


top Margin : 200

left Margin : 200


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");

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//lp.setMargins(200,100,0,0);
lp.leftMargin = 200;
lp.topMargin = 200;
btn.setLayoutParams(lp);


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


결과화면

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