Java 所选cardview无前景
我需要像谷歌这样处理笔记 当我选择一张便笺时会发生这种情况,而不仅仅是当我按下一张便笺时,即使我的手指不在便笺上,卡片视图也会被选中并呈灰色 我尝试使用自定义前景,但没有结果。Java 所选cardview无前景,java,android,android-cardview,notepad,Java,Android,Android Cardview,Notepad,我需要像谷歌这样处理笔记 当我选择一张便笺时会发生这种情况,而不仅仅是当我按下一张便笺时,即使我的手指不在便笺上,卡片视图也会被选中并呈灰色 我尝试使用自定义前景,但没有结果。 我不确定前台是否可以完成。请注意使用duplicateParentState。 childView将获取parentView的状态。 按下parentView时,子视图将获得按下状态 <FrameLayout android:layout_width="match_parent" andro
我不确定前台是否可以完成。请注意使用duplicateParentState。 childView将获取parentView的状态。 按下parentView时,子视图将获得按下状态
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:background="@drawable/bg_txt"
android:duplicateParentState="true"
android:textSize="15sp" />
</FrameLayout >
加
<color name="color_1">#4b14b1</color>
<color name="color_0">#ffffff</color>
#4b14b1
#ffffff
在colors.xml中
在drawable文件夹中添加bg_txt.xml
bg_txt.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@color/color_1"></item>
<item android:state_pressed="true" android:drawable="@color/color_1"></item>
<item android:drawable="@color/color_0"></item>
</selector>
尝试通过白灰色代码切换cardview的背景,具体取决于您喜欢的状态。Ie是否选中等?如果我理解正确,您是否希望在长时间单击时实现颜色更改和添加笔划?
你可以使用一个技巧:
按如下方式设置卡布局:
<android.support.v7.widget.CardView
android:id="@+id/card"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
...>
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
...>
...
</LinearLayout>
</android.support.v7.widget.CardView>
我还没有测试过这段代码,但我认为它应该可以做到这一点。即使我的手指不在上面,它也不会变色。这就是我要做的。。。
<android.support.v7.widget.CardView
android:id="@+id/card"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/your_color"
...>
...
</LinearLayout>
</android.support.v7.widget.CardView>
layout.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View l) {
card.setCardBackgroundColor(Color.parseColor("#939393")); //darker gray
layout.setBackgroundColor(Color.parseColor("#d4d4d4")) //gray
// use this to define margins in dp, not px
int dp8 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
// get layout's parent (card is parent of layout)
CardView.LayoutParams params = (CardView.LayoutParams) card.getLayoutParams();
// set margins in dp
params.setMargins(dp8, dp8, dp8, dp8);
// apply them to your layout
layout.setLayoutParams(params);
}
});