web-dev-qa-db-ja.com

プログラムでLinearLayoutにリップル効果を設定するにはどうすればよいですか?

背景を設定したいAndroid.R.attr.selectableItemBackgroundLinearLayoutに変換します。 XMLを使用する場合、問題はありません(動作します)

<LinearLayout
    Android:id="@+id/llMiner"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:background="?android:attr/selectableItemBackground"
    Android:clickable="true" >

...しかし、Javaコードでこれを行う必要があるため、これを試しました

llMiner.setClickable(true);
llMiner.setBackgroundResource(Android.R.attr.selectableItemBackground);

...そして、それは動作しません、実際、私はこの2行目にNotFoundExceptionを取得します。したがって、リソースがカラーであると考えてこのバリアントを試した後。

llMiner.setClickable(true);
llMiner.setBackgroundColor(Android.R.attr.selectableItemBackground);

これは例外を起動しませんが、...機能しません(押すときに背景の変更はありませんが、必要に応じて状態が変更されます)...何か提案はありますか?

39
Splact

このように使用できます。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // If we're running on Honeycomb or newer, then we can use the Theme's
    // selectableItemBackground to ensure that the View has a pressed state
    TypedValue outValue = new TypedValue();
    this.getTheme().resolveAttribute(Android.R.attr.selectableItemBackground, outValue, true);
    textView.setBackgroundResource(outValue.resourceId);
}
99
Wooseong Kim