2016-04-23 27 views
2

私のアプリにフローティングボタンがあります。私の浮動ボタンのXMLレイアウトは以下のAndroidのフローティングボタンの変更可能な描画イメージ

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    > 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     > 


     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      /> 

    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 


<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

</android.support.v4.widget.NestedScrollView> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/chrono_btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="@dimen/fab_margin" 
    android:clickable="true" 
    android:src="@drawable/ic_add_black_48dp" 
    app:layout_anchor="@+id/appbar" 
    app:layout_anchorGravity="bottom|right|end" /> 

</android.support.design.widget.CoordinatorLayout> 

何私がしたいことは代が発生したときにプログラム的浮動ボタン画像srcを変更することです。どのような私がこれまで試したことは

chrono_button.setBackground(getDrawable(R.drawable.ic_done_black_48dp)); 

ですが、私はいつもこのエラー I/FloatingActionButton取得:背景がサポートされていないカスタムの設定を。

私のコードで何が間違っているのですか、フローティングボタンの描画可能なイメージをプログラム的に変更するための方法はありますか?

答えて

0

背景を変更する必要はありません。イメージを設定します。良い明確化であるかもしれない

floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_full_sad)); 
1

一つの詳細はsetImageDrawable方法がFloatingActionButtonクラスのためではなくViewのために利用可能です。ですから、この方法を見つけるの問題が発生している場合は、単にファブなどをキャスト:それはあなたのソリューションを説明するために、より便利であり、それは単にコードの1行の答えを残すのではなく、どのように動作するか

((FloatingActionButton)mItemPlaying.getRootView().findViewById(R.id.fab)).setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_play_arrow_white_24dp)); 
0
fab.setImageResource(R.drawable.yourimagename); 
+2

。 – ajon

+0

@ajon私は同意しない。 OPの質問は本質的に働いていないコードの1行についてでした。訂正された行*は答えです。 –

関連する問題