相对布局中的图像按钮android没有显示图像

时间:2015-03-02 09:46:12

标签: android user-interface android-relativelayout

当图像处于相对布局时,我无法看到图像按钮的图像。如果我使用不同的布局,图像工作正常。 我的布局如下:

<?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

1 个答案:

答案 0 :(得分:0)

如果要实现浮动按钮,更好的方法是使用现有库:

在build.gradle依赖项中添加以下行:

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>

如果您想自定义浮动按钮,例如更改颜色,请将此行添加为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"