Programming

투명한 ImageButton을 사용하는 방법 : Android

procodes 2020. 2. 14. 23:48
반응형

투명한 ImageButton을 사용하는 방법 : Android


<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@drawable/transparent"></ImageButton>

이것이 SurfaceView에 해당 버튼을 배치하기 위해 투명한 ImageButton을 얻으려고했던 것입니다. 그러나 Eclipse는 XML에 투명 선을 포함하자마자 프로젝트에서 오류를 발생시킵니다.

도와주세요.


배경에 null을 사용해보십시오 ...

android:background="@null"

버튼 또는 일반보기가 클릭시 더 이상 강조 표시되지 않으므로 TRANSAPENT 또는 NULL LAYOUT을 사용하지 마십시오 !!!

나는 같은 문제가 있었고 마침내 안드로이드 API 에서 문제를 해결하기 위해 올바른 속성을 찾았습니다 . 모든보기에 적용 할 수 있습니다.

버튼 사양에서 이것을 사용하십시오.

android:background="?android:selectableItemBackground"

투명한 색상을 사용할 수도 있습니다.

android:background="@android:color/transparent"

배경을 설정하면 "@null"버튼을 클릭해도 효과가 없습니다. 이것이 더 나은 선택이 될 것입니다.

style="?android:attr/borderlessButtonStyle"

나중에 나는

android:background="?android:attr/selectableItemBackground"

또한 좋은 해결책입니다. 그리고이 속성을 자신의 스타일로 상속 할 수 있습니다.


런타임에 다음 코드를 사용할 수 있습니다

btn.setBackgroundDrawable(null);

허용되는 답변은 다음과 같아야합니다. android:background="?attr/selectableItemBackground"

이것은 @ lory105의 답변과 동일하지만 최대 호환성을 위해 지원 라이브러리를 사용합니다 ( android:동일한 것은 API> = 11에만 사용 가능)


이 줄을 제거하십시오 :

android:background="@drawable/transparent">

그리고 당신의 활동 수업에서

ImageButton btn = (ImageButton)findViewById(R.id.previous);
btn.setAlpha(100);

알파 레벨 0을 255로 설정할 수 있습니다

o는 투명을 의미하고 255는 불투명을 의미합니다.


가장 좋은 방법은 투명 색상 코드를 사용하는 것입니다

android:background="#00000000"

모든 것을 투명하게 만들기 위해 색상 코드 # 00000000을 사용하십시오.


사용 ImageView... 기본적으로 투명한 배경을 가지고 있습니다 ...


이것을 사용하십시오 :

<ImageButton
 android:id="@+id/back"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@null"
 android:padding="10dp"
 android:src="@drawable/backbtn" />

XML에서 ImageButton의 배경을 @null로 설정

<ImageButton android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/media_skip_backward"
android:background="@null"></ImageButton>

"@null"을 사용하십시오 . 그것은 나를 위해 일했다.

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/bkash"
    android:id="@+id/bid1"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@null" />

이것의 android:background="@android:color/transparent"

<ImageButton
    android:id="@+id/imageButton"
    android:src="@android:drawable/ic_menu_delete"
    android:background="@android:color/transparent"
/>

나는 이미 배경에 무언가를 추가하고 있었기 때문에이 일은 나를 위해 일했다.

   android:backgroundTint="@android:color/transparent"

(Android Studio 3.4.1)

편집 : 안드로이드 API 레벨 21 이상에서만 작동합니다. 호환성을 위해 대신 이것을 사용하십시오

   android:background="@android:color/transparent"

프로그래밍 방식으로 배경색을 투명하게 설정합니다.

 ImageButton btn=(ImageButton)findViewById(R.id.ImageButton01);
 btn.setBackgroundColor(Color.TRANSPARENT);

프로그래밍 방식으로 다음을 수행 할 수 있습니다.

image_button.setAlpha(0f) // to make it full transparent
image_button.setAlpha(0.5f) // to make it half transparent
image_button.setAlpha(0.6f) // to make it (40%) transparent
image_button.setAlpha(1f) // to make it opaque

XML에서 Background속성을 모든 White(#FFFFFF)색조 또는 음영으로 설정하십시오. Black(#000000)투명도를 원하면 실제 해시 코드 앞에 80을 넣으십시오.

#80000000   

<ImageButton
    android:id="@+id/previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/media_skip_backward">
</ImageButton>

에 투명 png사용 ImageButton했고 ImageButton효과가있었습니다.

참고 URL : https://stackoverflow.com/questions/3402787/how-to-have-a-transparent-imagebutton-android



반응형