0

所以我通过 gabrielemariotti 实现了浮动操作按钮 (FAB),我不知道这是否只是因为我在模拟器中运行它还是什么,但这就是它最终的样子:

http://imgur.com/iD1mU7Z

现在我正在使用一个图像作为加号,但它是我自己制作的。这是我编的图:

http://imgur.com/CqCLAop

^ 它在那里......

这也是我目前xml对该布局的看法:

<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"
tools:context=".DataActivity$DataFragment">
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/CardView_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:onClick="CardviewToast"
    card_view:cardCornerRadius="2dp"
    android:elevation="2dp">
    <TextView
        android:text="Current Stats:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>

<ImageButton
    android:id="@+id/fab"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_width="@dimen/fab_size"
    android:layout_height="@dimen/fab_size"
    android:layout_gravity="bottom|right"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    android:background="@drawable/ripple"
    android:stateListAnimator="@anim/anim"
    android:src="@drawable/ic_action_add"
    android:elevation="4dp"
    />

编辑

我认为这与:

//Outline
    int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
    Outline outline = new Outline();
    outline.setOval(0, 0, size, size);
    findViewById(R.id.fabdata).setOutline(outline);

我的代码中没有,因为

java.lang.RuntimeException: Unable to start activity ComponentInfo{target.totalarchery/target.totalarchery.DataActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOutline(android.graphics.Outline)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOutline(android.graphics.Outline)' on a null object reference

这没有任何意义,因为我将它放在与示例相同的位置。

4

2 回答 2

0

搬家解决了

 //Outline
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
Outline outline = new Outline();
outline.setOval(0, 0, size, size);
findViewById(R.id.fabdata).setOutline(outline);

进入主要活动类,而不是驻留在片段中

于 2014-08-26T00:13:13.287 回答
0

Google 现在在支持库中提供浮动操作按钮。请参阅我对此问题的回答 Floating Action Button

于 2015-09-22T05:50:45.243 回答