Tôi đặt hình ảnh làm nút Backgroud, nhưng Nút không có điểm nhấn khi nhấp vào nút đó. Có cách nào để giải quyết nó không.Làm cách nào để làm nổi bật Nút?
Trả lời
Để đưa bạn đến đúng tài nguyên ở đây là tài liệu của StateList Drawable. Bạn chỉ cần tạo và xác định nó trong thư mục con res/drawable
và đặt nó làm nền nút của bạn.
Câu trả lời tuyệt vời! Bạn đang làm gì ở đây trong quá khứ? ;-) –
Xem here
Và cũng here cho Romain Guy của câu trả lời:
Trong res/drawable, tạo ra một tập tin gọi là ví dụ mybutton_background.xml và đặt một cái gì đó giống như bên này:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:drawable="@drawable/button_background_focus" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item android:drawable="@drawable/button_background_normal" />
</selector>
Sau đó, đặt này có thể vẽ được là t nền của nút của bạn với android:background="@drawable/mybutton_background"
Tôi có thể' Tôi truy cập trang web đó, vì tôi ở Trung Quốc .... cảm ơn. – Nick
Bạn có thể sử dụng máy chủ proxy để đến đó. Dù sao, cập nhật câu trả lời của tôi với các giải pháp (một trong những viết bởi Romain Guy) –
nếu bạn đang sử dụng
<Button
------------------
android:background="@drawable/youimage"
>
Sau đó, vì nó là tốt hơn để tuyên bố mới một xml mới. với hình ảnh bạn có thể chỉ định hình ảnh cho mỗi event state. như Octavian nói
và bạn có thể bạn có thể đặt xml này như bạn nền
nếu xml của bạn là 'res/drawable/abc.xml' sau đó thiết lập nền như
android:background="@drawable/abc"
Bạn cũng có thể sử dụng ImageButton
<ImageButton
----------------------
android:src="@drawable/youimage"
/>
Lợi ích cho ImageButton là, không cần hình ảnh khác để làm nổi bật.
Sử dụng View.setFocusableInTouchMode hoặc android: focusableInTouchMode. Trong trường hợp này, hãy sử dụng phần này cho Nút của bạn, trong XML hoặc trong chính mã đó. Để biết chi tiết tham khảo: http: //developer.android.com/reference/android/view/View.html
Tôi là Trung Quốc, có GFW ở đây, vì vậy tôi không thể http://developer.android.com/ cảm ơn anyway – Nick
tôi đã thực hiện nó bằng cách ghi đè Nút lớp
public class MButton extends Button {
public MButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
//define style
public void setPressedBg(Integer[] mImageIds) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = this.getResources().getDrawable(mImageIds[0]);
Drawable selected = this.getResources().getDrawable(mImageIds[1]);
Drawable pressed = this.getResources().getDrawable(mImageIds[2]);
bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
bg.addState(View.ENABLED_STATE_SET, normal);
bg.addState(View.FOCUSED_STATE_SET, selected);
bg.addState(View.EMPTY_STATE_SET, normal);
this.setBackgroundDrawable(bg);
}
//define style
public void setPressedBg(Integer p1,Integer p2,Integer p3) {
StateListDrawable bg = new StateListDrawable();
Drawable normal = this.getResources().getDrawable(p1);
Drawable selected = this.getResources().getDrawable(p2);
Drawable pressed = this.getResources().getDrawable(p3);
bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
bg.addState(View.ENABLED_FOCUSED_STATE_SET, selected);
bg.addState(View.ENABLED_STATE_SET, normal);
bg.addState(View.FOCUSED_STATE_SET, selected);
bg.addState(View.EMPTY_STATE_SET, normal);
this.setBackgroundDrawable(bg);
}
}
trong tôi main()
toneButton.setPressedBg(R.drawable.button_tone_protrait_pipa,
R.drawable.button_tone_protrait_pipa1,
R.drawable.button_tone_protrait_pipa1);
Hãy thử điều này để có được hiệu ứng làm nổi bật của người nghèo mà không cần tạo nhiều bản sao hình ảnh của bạn. Thiết lập giá trị alpha 0,8 đến một giá trị mong muốn:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageButton;
public class EAImageButton extends ImageButton {
public EAImageButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public EAImageButton(Context context, AttributeSet attrs){
super(context,attrs);
}
public EAImageButton(Context context, AttributeSet attrs, int defStyle){
super(context,attrs,defStyle);
}
public void setImageResource (int resId){
super.setImageResource(resId);
}
@Override
public boolean onTouchEvent(MotionEvent e){
if(e.getAction() == MotionEvent.ACTION_DOWN){
this.setAlpha((int)(0.8 * 255));
}else if(e.getAction() == MotionEvent.ACTION_UP){
this.setAlpha((int)(1.0 * 255));
}
return super.onTouchEvent(e);
}
}
+100 Sạch sẽ và thông minh. –
Nếu đây là một ImageButton và bạn không muốn sử dụng một vài drawable cho mỗi ép bang/unpressed bạn có thể sử dụng attibute lọc màu sắc của hình ảnh. Giải pháp này tương tự như giải pháp được Omar sử dụng.
Tạo OnTouchListener sửa đổi bộ lọc màu khi chạm.
public class ButtonHighlighterOnTouchListener implements OnTouchListener {
final ImageButton imageButton;
public ButtonHighlighterOnTouchListener(final ImageButton imageButton) {
super();
this.imageButton = imageButton;
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
//grey color filter, you can change the color as you like
imageButton.setColorFilter(Color.argb(155, 185, 185, 185));
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageButton.setColorFilter(Color.argb(0, 185, 185, 185));
}
return false;
}
}
Gán nghe này để nút của bạn:
myButton = (ImageButton) findViewById(R.id.myButton);
myButton.setOnTouchListener(new ButtonHighlighterOnTouchListener(myButton));
Cập nhật
Cải thiện lớp để áp dụng highlighter để ImageView, ImageButton hoặc TextView qua drawable hợp chất của nó.
public class ButtonHighlighterOnTouchListener implements OnTouchListener {
private static final int TRANSPARENT_GREY = Color.argb(0, 185, 185, 185);
private static final int FILTERED_GREY = Color.argb(155, 185, 185, 185);
ImageView imageView;
TextView textView;
public ButtonHighlighterOnTouchListener(final ImageView imageView) {
super();
this.imageView = imageView;
}
public ButtonHighlighterOnTouchListener(final TextView textView) {
super();
this.textView = textView;
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
if (imageView != null) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
imageView.setColorFilter(FILTERED_GREY);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
imageView.setColorFilter(TRANSPARENT_GREY); // or null
}
} else {
for (final Drawable compoundDrawable : textView.getCompoundDrawables()) {
if (compoundDrawable != null) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
// we use PorterDuff.Mode. SRC_ATOP as our filter color is already transparent
// we should have use PorterDuff.Mode.LIGHTEN with a non transparent color
compoundDrawable.setColorFilter(FILTERED_GREY, PorterDuff.Mode.SRC_ATOP);
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
compoundDrawable.setColorFilter(TRANSPARENT_GREY, PorterDuff.Mode.SRC_ATOP); // or null
}
}
}
}
return false;
}
}
Chỉ cần một chỉnh sửa: Bạn phải trả lại true trên onTouch() để báo cho SO biết rằng bạn đã thực hiện một số hành động trong hành động chạm. –
đây là những gì tôi đang tìm kiếm .. imageView.setColorFilter() hoạt động hoàn hảo –
- 1. Làm nổi bật NSToolbarItems
- 2. làm nổi bật nút 'quay lại' trên bàn phím iOS
- 3. Chế độ Emacs C - làm cách nào để bạn làm nổi bật số hex?
- 4. Tôi làm cách nào để làm nổi bật hàng của bảng khi nhấp?
- 5. Làm cách nào để làm nổi bật các phần của đồ họa trên di chuột?
- 6. Trong vim, làm cách nào để làm nổi bật TODO: và FIXME :?
- 7. Làm cách nào để làm nổi bật Chế độ xem tùy chỉnh trong XML?
- 8. C# - Cách: Mở PDF, làm nổi bật kết quả tìm kiếm và lưu làm nổi bật trở lại PDF
- 9. Làm nổi bật nhãn Windows Forms
- 10. Làm nổi bật sửa đổi bộ đệm
- 11. thay đổi cách Nimbus LaF xử lý nút JTree làm nổi bật
- 12. Nhấp đúp vào Làm Nổi bật
- 13. Inline đang làm nổi bật trong reStructuredText
- 14. Cách xóa nút mặc định làm nổi bật trong Safari khi sử dụng jQuery
- 15. gvim chuỗi multiline dài làm nổi bật
- 16. Làm nổi bật cú pháp trong vim
- 17. Làm nổi bật cú pháp Wordpress
- 18. Làm nổi bật đường trong biểu đồ nổi
- 19. Tùy chỉnh NSButtonCell bằng hình ảnh và làm nổi bật
- 20. Tùy chỉnh UIButton không làm nổi bật khi chạm vào
- 21. Macro Microsoft Word để làm nổi bật nhiều từ
- 22. Làm thế nào để vô hiệu hóa UIButton nổi bật trên nhấp chuột?
- 23. Xóa WPF ListView/GridView làm nổi bật chrome
- 24. Làm thế nào để vô hiệu hóa làm nổi bật của biểu tượng ứng dụng?
- 25. Dừng IE khỏi làm nổi bật nút gửi đầu tiên theo biểu mẫu
- 26. NSButton không có "nhấp vào làm nổi bật"
- 27. Làm thế nào để làm nổi bật nhiều lựa chọn? Ví dụ
- 28. Làm thế nào để làm nổi bật "có gì mới" giữa hai tài liệu latex
- 29. Làm cách nào để triển khai biểu tượng thanh công cụ nổi bật với Delphi?
- 30. Làm nổi bật cú pháp văn bản, mở rộng làm nổi bật từ một ngôn ngữ khác
Tham khảo này [link] (http://stackoverflow.com/questions/4125774/android-button-set-onclick-background-image-change-with-xml/4125877#4125877) – David