Tôi có thể đặt nền ImageButton trong suốt layout.xmlbằng cách sử dụng:
android:background="@android:color/transparent"
Làm cách nào để tôi có thể biên dịch cùng một thứ bằng cách sử dụng mã java? Cái gì đó nhưib.setBackgroundColor(???);
Tôi có thể đặt nền ImageButton trong suốt layout.xmlbằng cách sử dụng:
android:background="@android:color/transparent"
Làm cách nào để tôi có thể biên dịch cùng một thứ bằng cách sử dụng mã java? Cái gì đó nhưib.setBackgroundColor(???);
Câu trả lời:
Đây là cách đơn giản chỉ bạn phải đặt màu nền là trong suốt
ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
btn.setBackgroundColor(Color.TRANSPARENT);
Làm điều đó trong xml của bạn
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButtonSettings"
android:layout_gravity="right|bottom"
android:src="@drawable/tabbar_settings_icon"
android:background="@android:color/transparent"/>
Điều này sẽ hoạt động - imageButton.setBackgroundColor(android.R.color.transparent);
KHÔNG SỬ DỤNG TRANSAPENT HOẶC NULL LAYOUT vì khi đó button(hoặc chế độ xem chung) sẽ không còn nổi bật khi nhấp chuột !!!
Tôi đã gặp vấn đề tương tự và cuối cùng tôi đã tìm thấy thuộc tính chính xác từ Android API để giải quyết vấn đề. Nó có thể áp dụng cho mọi chế độ xem
Sử dụng điều này trong thông số kỹ thuật của nút
android:background="?android:selectableItemBackground"
Điều này yêu cầu API 11
nullhoặc transparenttắt phản hồi của nút.
chỉ cần sử dụng điều này trong bố cục nút hình ảnh của bạn
android:background="@null"
sử dụng
android:background="@android:color/transparent
hoặc là
btn.setBackgroundColor(Color.TRANSPARENT);
không cho độ trong suốt hoàn hảo
Nếu bạn muốn sử dụng Rlớp android
textView.setBackgroundColor(ContextCompat.getColor(getActivity(), android.R.color.transparent));
và đừng quên thêm thư viện hỗ trợ vào tệp Gradle
compile 'com.android.support:support-v4:23.3.0'