programing

버튼 배경이 투명함

codeshow 2023. 8. 5. 11:06
반응형

버튼 배경이 투명함

단추가 있어요.버튼을 누를 때 텍스트를 굵게 표시해야 합니다. 그렇지 않으면 정상입니다.그래서 저는 볼드 & 노멀 스타일을 썼습니다.

<style name="textbold" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">bold</item>
</style>
<style name="textregular" parent="@android:style/TextAppearance">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textStyle">normal</item>
</style>

이제 button_states.xml을 다음과 같이 사용합니다.

<?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="true"
    style="@style/textbold" />
<item android:state_focused="false" android:state_pressed="true"
    style="@style/textregular" />

<item style="@style/textregular" />
</selector> 

이 버튼의 레이아웃에서 배경도 투명하게 만들어야 합니다.어떻게 할까요?레이아웃 코드:

<Button android:id="@+id/Btn" android:background="@drawable/button_states" />

내 스타일에 배경을 투명하게 포함하려면 어떻게 해야 합니까?

배경을 투명하게 하려면 다음을 수행합니다.android:background="@android:color/transparent".

하지만 선택기를 정말 이상한 방식으로 사용하고 있기 때문에 문제가 좀 더 깊어진 것 같습니다.이 잘못된 것 .<item/>.

Android 소스에서 스타일이 어떻게 사용되는지 자세히 살펴봅니다.버튼을 클릭해도 텍스트 스타일을 변경하지는 않지만, 목표를 달성하는 방법에 대한 좋은 아이디어가 많이 있습니다.

배경을 투명하게 설정하는 새로운 방법 시도

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

xml에 다음을 사용할 수도 있습니다.

android:background="@null"

또는 코드:

buttonVariable.setBackgroundColor(Color.TRANSPARENT);

사용하다#0000(과 4개의 0만 (0과 4가 동일한 값으로 black 입니다.) 투명 색상 코드입니다.직접 사용할 수 있지만 color.xml에 색상을 정의하여 코드를 재사용할 수 있도록 하는 것이 좋습니다.

Xml에 추가합니다.android:background="@android:color/transparent"

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button"
            android:background="@android:color/transparent"
            android:textStyle="bold"/>

XML을 사용하여 이를 달성했습니다.

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

사용한

btn.setBackgroundColor(Color.TRANSPARENT);

그리고.

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

선택기는 그리기 가능한 경우에만 작동하고 스타일은 작동하지 않습니다.참조


먼저 단추 배경을 투명하게 만들려면 재료 설계 애니메이션에 영향을 주지 않으므로 다음 특성을 사용합니다.

style="?attr/buttonBarButtonStyle"

단추에 스타일을 지정하는 방법은 여러 가지가 있습니다. 튜토리얼을 확인하십시오.

번째로, 누른 상태에서 텍스트를 굵게 표시하려면 다음 Java 코드를 사용합니다.

btn.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
            // When the user clicks the Button
            case MotionEvent.ACTION_DOWN:
                btn.setTypeface(Typeface.DEFAULT_BOLD);
                break;

            // When the user releases the Button
            case MotionEvent.ACTION_UP:
                btn.setTypeface(Typeface.DEFAULT);
                break;
        }
        return false;
    }
});

우리는 아래와 같이 버튼 xml에서 안드로이드:background 속성을 사용할 수 있습니다.

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

아니면 우리는 스타일을 사용할 수 있습니다.

style="?android:attr/borderlessButtonStyle"투명하고 그림자 없는 배경을 위해.

색상을 알파 채널로 설정하면 이를 달성할 수 있습니다.

색상표는 #ARRGGBB와 같습니다. 여기서 A는 알파 채널(투명도), R은 빨간색, G는 녹색, B는 파란색을 나타냅니다.

1단계: 그리기 가능한 새 리소스 파일을 만들고 복사 붙여넣기

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <stroke android:color="#fff" android:width="2dp"/>
    <corners android:radius="25dp"/>
    <padding android:right="15dp" android:top="15dp" android:bottom="15dp" android:left="15dp"/>
</shape>

단추로 저장UI(예를 들어)

2단계: UI를 xml 버튼에 적용합니다.

<Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="join the crew"
  android:background="@drawable/ButtonUI"
  android:textColor="#fff"/>

해 주세요.Borderless

<style name="Button" parent="Widget.AppCompat.Button.Borderless">
    <item name="android:textColor">@color/blue</item>
    <item name="android:textAllCaps">true</item>
</style>

코드:

button.setVisibility(View.INVISIBLE);

Xml:

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

xml 파일에 아래 속성을 추가하면 쉽게 할 수 있습니다.이 코드는 많은 시간 동안 테스트되었습니다.

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

배경색을 다음과 같이 적용합니다.transparent(light gray)버튼을 클릭합니다.

ButtonName.setOnClickListener()

위의 방법으로 버튼의 배경색을 설정합니다.

언급URL : https://stackoverflow.com/questions/4954102/button-background-as-transparent

반응형