안드로이드 개발 질문/답변
(글 수 45,052)
뷰를 상속받아서 자바코드로 레이아웃을 그려 버튼으로 쓸 커스텀뷰를 만들었습니다.
그리고 액티비티쪽에서 객체생성을 통해 그렸는데 그 그린부분에 터치이벤트를 주어 커스텀뷰의 백그라운드색깔을 바꾸고 싶은데 어떻게 터치이벤트를 주어야 할지 모르겠네요.
액티비티에 온터치이벤트를 주니 액티비티전체가 먹혀버리네요.
도와주세요.
ButtonTestActivity.java
package kr.gnu.cs.ButtonTest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class ButtonTestActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mMainLinearLayout = (LinearLayout) findViewById(R.id.mainLinearLayout);
CustomButton mCustomButton = new CustomButton(this, mMainLinearLayout);
}
}
CustomButton.java
package kr.gnu.cs.ButtonTest;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomButton extends View {
private LinearLayout whole_ll = null;
private LinearLayout left_ll = null;
private LinearLayout.LayoutParams left_ll_params = null;
private LinearLayout right_ll = null;
private LinearLayout.LayoutParams right_ll_params = null;
private RelativeLayout right_ll_in_rl = null;
private RelativeLayout.LayoutParams right_ll_in_rl_params = null;
private TextView menuText = null;
private RelativeLayout.LayoutParams menuText_params;
public CustomButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public CustomButton(Context context, LinearLayout linearlayout) {
super(context);
whole_ll = new LinearLayout(context);
LinearLayout.LayoutParams whole_ll_params = new LinearLayout.LayoutParams(
300, 100);
whole_ll.setLayoutParams(whole_ll_params);
whole_ll.setOrientation(0);
linearlayout.addView(whole_ll);
left_ll = new LinearLayout(context);
left_ll_params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 9.0f);
left_ll.setLayoutParams(left_ll_params);
left_ll.setBackgroundResource(android.R.color.darker_gray);
whole_ll.addView(left_ll);
right_ll = new LinearLayout(context);
right_ll_params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f);
right_ll.setLayoutParams(right_ll_params);
whole_ll.addView(right_ll);
right_ll_in_rl = new RelativeLayout(context);
right_ll_in_rl_params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
right_ll_in_rl.setLayoutParams(right_ll_in_rl_params);
right_ll_in_rl.setBackgroundResource(android.R.color.white);
right_ll.addView(right_ll_in_rl);
menuText = new TextView(context);
menuText_params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
menuText_params.addRule(RelativeLayout.CENTER_IN_PARENT);
menuText.setLayoutParams(menuText_params);
menuText.setText("테스트입니다");
right_ll_in_rl.addView(menuText);
}
}
OnTouchListener 등록하시고 메소드 오버라이딩 하시고 위에 리소스 case R.id.mainLinearLayout:
이렇게해서 setBackgroundR~(R.dr...)ㅎㅏ셔도 될것 같고
mCustomButton.setOnTouchListener(new OnTouchListener){
이렇게 하셔도 될것 같은데요