new*_*our 56 android android-cardview
简单来说:
我们如何定义CardView的cardBackgroundColor属性的颜色状态(在这种情况下,在ListView布局中)?
(我在安装了4.4的手机上使用了Android L开发者预览版的RC1,在build.gradle中使用了"com.android.support:cardview-v7:21.0.0-rc1")
更长:
在CardView布局中,我们通过cardCornerRadius和cardBackgroundColor设置CardView的角半径和背景颜色.
但是,例如,背景颜色不会被视为选择状态,即是否按下了列表项.
但是,如果在CardView的内部视图中设置了背景颜色和相关的状态,则它将显示在您在CardView中定义的角上.
那么,我们如何才能确保CardView的cardBackgroundColor中的状态得到尊重?
这是cardBackgroundColor,colour_with_states.xml使用的颜色:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:color="@android:color/holo_green_dark" />
<item android:state_focused="true" android:state_enabled="false" android:color="@android:color/holo_green_dark" />
<item android:state_focused="true" android:state_pressed="true" android:color="@android:color/holo_green_dark" />
<item android:state_focused="false" android:state_pressed="true" android:color="@android:color/holo_green_dark" />
<item android:state_focused="true" android:color="@android:color/holo_green_dark" />
<!-- Only this below is seen in the cardview dispaly -->
<item android:color="@android:color/holo_blue_bright" />
</selector>
Run Code Online (Sandbox Code Playgroud)
以及使用CardView的布局:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
cardview:cardCornerRadius="10dp"
cardview:cardBackgroundColor="@color/colour_with_states"
>
<!-- If we set a background color below, it will overwrite our radius defined above -->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:text="Lorem ipsum"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItem"
android:background="@null"
android:gravity="center_vertical"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:paddingStart="8dip"
android:paddingEnd="8dip"
/>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
too*_*o42 64
虽然这不是理想的,但由于边缘不是圆角,您可以添加触摸反馈,CardView
如下所示:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
//Nested View ...
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
添加android:foreground
和android:clickable
属性CardView
.
此外,这会产生负面影响,因为该android:clickable
属性会覆盖任何clickListener,因此不会触发那些clickListener.
我有一些CardView实现的例子
循环(https://github.com/lawloretienne/Loop) - https://github.com/lawloretienne/Loop/blob/master/app/src/main/res/layout/category_card.xml
QuickReturn(https://github.com/lawloretienne/QuickReturn) - https://github.com/lawloretienne/QuickReturn/blob/master/sample/src/main/res/layout/activity_quick_return.xml
经过更多的研究,我在所有API版本(包括pre-Lollipop)上为CardViews提供了一个很好的解决方案.
https://medium.com/@etiennelawlor/layout-tips-for-pre-and-post-lollipop-bcb2e4cdd6b2#.9h0v1gmaw
Ant*_*ard 11
有时,您可能希望CardView
获得视觉触摸反馈.该android:foreground="?android:attr/selectableItemBackground"
解决方案是为这个完美的.
但是,您可以考虑使用drawSelectorOnTop(true)
ListView.这根本不需要改变CardView
.
如果需要进一步澄清,请与我们联系.
这是我解决问题的方法.
首先,创建一个名为CustomCardView
extends 的自定义类CardView
然后覆盖该drawableStateChanged()
方法,setCardBackgroundColor()
当卡的按下状态改变时,通过调用方法更改卡背景颜色.
最后,在布局文件中使用此CustomCardView替换CardView.
这个解决方案唯一的缺点是cardview无法在Android 5.0及更高版本上显示涟漪效果.
这是我的代码:
public class CustomCardView extends CardView {
public CustomCardView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public CustomCardView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public CustomCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (isPressed()) {
this.setCardBackgroundColor(getContext().getResources().getColor(R.color.card_view_pressed));
} else {
this.setCardBackgroundColor(getContext().getResources().getColor(R.color.card_view_normal));
}
}
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
49565 次 |
最近记录: |