안드로이드 개발 질문/답변
(글 수 45,052)
XML 디자인 모드가 아니라
코드를 이용하여 런타임때 버튼을 이동시키려고 하는데요.
Padding값을 변경 시키면 버튼이 이동하는게 아니라
버튼과 버튼에 쓰여진 Text사이에 간격이어서
버튼이 고정되는 것이 아니라 쭉쭉 들어나기만 하고
이동은 안되네요... 휴..... 혹시 제가 잘못알고 있는 건가요?
도와주세요~ ㅠ.ㅠ
코드를 이용하여 런타임때 버튼을 이동시키려고 하는데요.
Padding값을 변경 시키면 버튼이 이동하는게 아니라
버튼과 버튼에 쓰여진 Text사이에 간격이어서
버튼이 고정되는 것이 아니라 쭉쭉 들어나기만 하고
이동은 안되네요... 휴..... 혹시 제가 잘못알고 있는 건가요?
도와주세요~ ㅠ.ㅠ
2009.09.07 11:43:09
헉.. 해봤는데.. 부모 padding을 조절해도 똑같네요. 어짜피 부모만 padding된거고 padding된 곳에 똑같이 자식이 붙어있으니....
layoutMargin도 소스코드에서 조절할 수 있나요?
layoutMargin도 소스코드에서 조절할 수 있나요?
2009.09.07 11:49:14
원래 의도는 실시간으로 버튼을 아래처럼 이동 시키려고 하는데
padding을 변경하면 부모에 view대한 position이 바뀌는게 아니라
그냥 자기 자신 버튼의 위치에서 바뀌는 거라서 그냥 늘어나 버리네요....
소스코드에서 바꾸려고 하는데...
Margin을 변경하는게 있으면 참좋을텐데...
도와주세요~ 3일째 밤새고 해결 못했네요..
padding을 변경하면 부모에 view대한 position이 바뀌는게 아니라
그냥 자기 자신 버튼의 위치에서 바뀌는 거라서 그냥 늘어나 버리네요....
소스코드에서 바꾸려고 하는데...
Margin을 변경하는게 있으면 참좋을텐데...
도와주세요~ 3일째 밤새고 해결 못했네요..
2009.09.07 15:23:57
public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.button); ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams( button.getLayoutParams()); margin.setMargins(50, 5, 5, 5); button.setLayoutParams(new LinearLayout.LayoutParams(margin)); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("LOG", "Main.onClick"); Button button = (Button) v; ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams( button.getLayoutParams()); margin.setMargins(250, 5, 5, 5); button.setLayoutParams(new LinearLayout.LayoutParams(margin)); button.invalidate(); } }); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:background="#FFFFFF" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:background="#FF0000" android:text="BUTTON" /> </LinearLayout>
자바 클래스에서 margin을 추가하는 방법입니다.
버튼을 클릭하면 margin을 늘려서 이동합니다.