2015-03-02 63 views
0

當圖像處於相對佈局時,我無法看到圖像按鈕的圖像。如果我使用不同的佈局,圖像工作正常。 我的佈局如下:相對佈局中的圖像按鈕android not shown the image

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/cardList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
    <ImageButton 
    android:layout_width="56dp" 
    android:layout_height="56dp" 
    android:src="@drawable/plus" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginRight="16dp" 
    android:layout_marginBottom="16dp" 
    android:id="@+id/fab" 
    android:elevation="1dp" 
    /> 

</RelativeLayout> 

Image to show the layout and image button

相同的圖像,如果我在其他一些佈局設置的東西做工精細。任何人都可以給我一些指引,看看哪裏。 日誌中沒有錯誤或警告。 實際png圖片如下: plus.png

+0

你想實現浮動按鈕? – 2015-03-02 09:47:30

+0

是試驗浮動按鈕。 – Thebestshoot 2015-03-02 09:51:24

+0

您可以使用框架佈局。 – 2015-03-02 10:17:28

回答

0

如果你想實現浮動按鈕,更好的方式將使用現有的庫:

添加此行對你的build.gradle dependencises:

compile 'com.getbase:floatingactionbutton:1.3.0' 

,改變你的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:paddingBottom="@dimen/activity_vertical_margin"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/cardList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

    <com.getbase.floatingactionbutton.AddFloatingActionButton 
     android:id="@+id/common_ring_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginBottom="10dp" 
     android:layout_marginEnd="10dp" 
     android:layout_marginRight="10dp"/> 

</RelativeLayout> 

,如果你想定製你的FL例如改變顏色浮動按鈕,添加此行RelativeLayout屬性:

xmlns:fab="http://schemas.android.com/apk/res-auto" 

,並添加下面的屬性到AddFloatingActionButton XML

fab:fab_colorNormal="@color/floating_button_bg" 
fab:fab_colorPressed="@color/floating_button_bg_pressed" 
+0

我看起來更像是爲什麼這不是爲我工作,而不是隻是與一些有用的東西相處。 我實際上並沒有在任何應用中實現這種佈局,因爲我只是在試驗,所以想要了解爲什麼事情不起作用。 – Thebestshoot 2015-03-02 10:00:57

+0

它看起來像加可繪製只是白色的圓圈 - 沒有加 – 2015-03-02 10:03:26

+0

無論如何,我聽到很多人在stackoverflow和避免使用第三方庫的人,作爲一個新手,我不知道爲什麼。你對此有何看法?我只是想知道什麼是好的,爲什麼它是 – Thebestshoot 2015-03-02 10:03:39