4
사용자 지정 단추가 있고 프로그래밍 방식으로 누름 및 기본 색을 변경했습니다.Android StateListDrawable pressed 상태가 항상 마지막으로 표시됨
public class CustomApplicationButton extends Button {
public CustomApplicationButton(Context context) {
this(context, 0, 0, 0);
}
public CustomApplicationButton(Context context, int topDrawableResId, int outlineDefaultColorId, int outlinePressedColorId) {
super(context);
// set width and height
LinearLayout.LayoutParams params = new LayoutParams(
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_width),
context.getResources().getDimensionPixelSize(R.dimen.sr_application_button_height));
setLayoutParams(params);
// set drawable top icon
if (topDrawableResId != 0) {
setCompoundDrawablesWithIntrinsicBounds(0, topDrawableResId, 0, 0);
}
// set background and outline color
int strokeWidth = context.getResources().getDimensionPixelSize(R.dimen.sr_launcher_button_stroke_size);
// unpressed state drawable
LayerDrawable defaultLayers = (LayerDrawable) context.getResources().getDrawable(
R.drawable.btn_launcher_shape_default);
GradientDrawable defaultShapeOutline = (GradientDrawable) defaultLayers.findDrawableByLayerId(R.id.outline_default);
defaultShapeOutline.setStroke(strokeWidth, context.getResources().getColor(outlineDefaultColorId));
// pressed state drawable
LayerDrawable pressedLayers = (LayerDrawable) context.getResources().getDrawable(
R.drawable.btn_launcher_shape_pressed);
GradientDrawable pressedShapeOutline = (GradientDrawable) pressedLayers.findDrawableByLayerId(R.id.outline_pressed);
pressedShapeOutline.setStroke(strokeWidth, context.getResources().getColor(outlinePressedColorId));
// set states
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed}, pressedLayers);
states.addState(new int[] { }, defaultLayers);
// set background
this.setBackground(states);
}
은}
다음, 내부에있는 LinearLayout을 가지고 내 활동이 버튼을 추가했습니다.
public class MainActivity extends Activity {
private LinearLayout applicationPanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
applicationPanel = (LinearLayout) findViewById(R.id.application_panel);
ssPanel = (LinearLayout) findViewById(R.id.ss_panel);
CustomApplicationButton btnLauncherApp = new CustomApplicationButton(this, R.drawable.ic_application,
R.color.application_green_default_color, R.color.application_green_pressed_color);
btnLauncherApp.setText("Button 1");
applicationPanel.addView(btnLauncherApp);
btnLauncherApp = new CustomApplicationButton(this, R.drawable.ic_camera,
R.color.camera_blue_default_color, R.color.camera_blue_pressed_color);
btnLauncherApp.setText("Button 2");
applicationPanel.addView(btnLauncherApp);
btnLauncherApp = new CustomApplicationButton(this, R.drawable.ic_browser,
R.color.browser_gray_default_color, R.color.browser_gray_pressed_color);
btnLauncherApp.setText("Button 3");
applicationPanel.addView(btnLauncherApp);
}
}
내 문제는 3 버튼은 다른 기본 획 색상을 가지고 있지만 누르면 획 색상은 항상 마지막에 추가 버튼 색상 것입니다.
요약하면 다음과 같습니다.
이 그림보다 천 이상의 단어 :