2017-10-17 54 views
0

我正在開發一個Android應用程序,它有幾個(Next,Back,...等)Buttons這些都是圓形和透明的,我使用ImageButton那些Buttons ,但問題是Button(我使用黑色background,因此它非常難看)上總是有一個白色邊框,並且該按鈕從未顯示爲圓形,表現爲某種方形的形狀。無法擺脫ImageButton周圍醜陋的邊框

這裏是我試過到目前爲止:

  1. activity_mypage.xml文件設置ImageButton的背景

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

    ,並

    android:background="?android:selectableItemBackground" 
    

    android:background="?android:selectableItemBackgroundBorderless" 
    

    甚至

    android:background="@null" 
    

    但似乎沒有任何的.XML邊工作。

  2. ,我試圖在MyPage.java文件如下:

    myBtn.setBackgroundResource(0); 
    

    myBtn.setBackground(null); 
    

似乎沒有任何工作對我所做不斷產生相同的(儘管它從Button附近刪除灰色邊框,但沒有一個使得Button完全透明)

這是按鈕的截圖應用任何上述前:
Before Applying any of the above settings/attributes

而且這是同Button截圖應用任何這些屬性(的並不重要,如果以後我申請它只是在xml或僅java文件,或同時,因爲所有的結果都是一樣的):
enter image description here

這是在XML我Buttons我的一個XML代碼:

<ImageButton 
    android:id="@+id/my_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="26dp" 
    android:layout_marginEnd="37dp" 
    android:layout_marginRight="37dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:srcCompat="@mipmap/my_btn" 
    tools:layout_constraintBottom_creator="1" 
    tools:layout_constraintRight_creator="1" 
    android:background="@android:color/transparent" 
    /> 

這裏是在java文件它的代碼:

 ImageButton myBtn= (ImageButton) findViewById(R.id.my_btn); 
     myBtn.setBackground(null); 
     myBtn.setBackgroundResource(0); 

我真的失去了在這個每個結果我發現這個查詢已經提出的上述方法之一,但沒有這些似乎爲我工作..

+0

看起來你的圖像周圍有白色邊框。 –

+0

你可以發佈你的圖像(my_btn)也 – UltimateDevil

+0

@VivekMishra我曾嘗試過一些透明的圖像,但他們都有同樣的問題,[這裏](https://imgur.com/a/VZg3C)是這個鏈接我使用的相同圖像。 –

回答

0

只要改變

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

android:background="@null" 
+0

他做到了,但沒有奏效。 – nhoxbypass

+0

我已經嘗試了這兩個問題,因爲某些原因不起作用。 –

0

與此

android:src="@mipmap/yourimage" 

,而不是嘗試

app:srcCompat="@mipmap/my_btn"

+0

我只是試過,它也不工作.. –

+0

@LawaFazil你想要什麼樣的動畫? –

0

可以使用ImageView代替ImageButton,所以不會發生的背景問題

<ImageView 
      android:id="@+id/my_btn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="26dp" 
      android:layout_marginEnd="37dp" 
      android:layout_marginRight="37dp" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      android:src="@mipmap/my_btn" 
     /> 
+0

我希望點擊按鈕時點擊「動畫」,這就是爲什麼我用ImageButton –

+0

@LawaFazil告訴我,你有活動或活動AppCompactActivity延長你的活動類 – UltimateDevil

0

添加到您的按鈕

android:background="@drawable/template" 

,並在您繪製文件夾 創建一個文件命名爲模板,並添加這個代碼吧

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:state_pressed="true" android:drawable="@drawable/btn2imagePressed"></item> 
<item android:drawable="@drawable/btn1imageNotPressed"></item> </selector> 
+0

我發現是什麼原因造成的問題我將發佈解決方案現在.. –

0

所以讀完som張貼的答案e和在我的代碼我注意到,icon被錯誤地裝載到我mipmap目錄,而不是drawables,這是因爲當我進口icons我選擇New->Image Asset代替Vector Asset造成的建議玩弄我的「圖標'失去其透明度...

感謝大家的答覆和答案。