任何人都可以向我解释如何在CardView中实现Google I/O 2014中展示的一些视觉触摸反馈.
以下是我在XML中使用CardView的方法,可能有一些我想念的小东西,所以我只是想知道是否有人可以帮助我?
<!-- A CardView -->
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/CardView_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
card_view:cardCornerRadius="4dp"
android:elevation="2dp">
<LinearLayout
android:id="@+id/LinearLayout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="RunSomeMethod"">
<!-- Main CardView Content In Here-->
</LinearLayout> </android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud) android android-layout android-cardview android-5.0-lollipop
我在Nexus 5上运行它.这是我的CardView代码的一部分:
CardView cardView = new CardView(getActivity());
cardView.setRadius(4);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 400);
lp.setMargins(32, 16, 32, 16);
cardView.setLayoutParams(lp);
cardView.setContentPadding(50, 50, 50, 50);
...
cardView.setForeground(selectedItemDrawable);
Run Code Online (Sandbox Code Playgroud)
这是我如何获得selectedItemDrawable:
int[] attrs = new int[] { R.attr.selectableItemBackground };
TypedArray ta = getActivity().obtainStyledAttributes(attrs);
selectedItemDrawable = ta.getDrawable(0);
ta.recycle();
Run Code Online (Sandbox Code Playgroud)
当我点击卡片时,不会出现与selectedItemDrawable一起出现的波纹(它看起来与没有前景设置完全相同).我正在运行5.0,所以这看起来很奇怪,因为appcompat文档只说它不适用于pre-Lollipop设备.有人知道为什么会这样吗?最低API级别为16,目标为21.
android android-cardview rippledrawable android-5.0-lollipop