更改列表项背景边框的颜色
Changing color of the border of the background of a list item
我正在研究 this tutorial 并且有这样的想法,当你按下一个按钮时,其中一张卡片的边框会改变颜色。
我查看了 this solution,但没有任何反应。
这是我所做的:
我有一个 xml 可绘制形状 (feed_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#d8d8d8" />
<corners android:radius="7dp" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:id="@+id/feedsquare">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="@color/white" >
</solid>
<!-- view border color and width (I WANT TO CHANGE THIS) -->
<stroke
android:width="1dp"
android:color="@color/white" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
</shape>
</item>
</layer-list>
然后我的布局基本上就是每个 card/list 项的外观 (list_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:background="@drawable/feed_item"
android:id="@+id/card"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="2dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/title"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMM"
android:textSize="@dimen/name3"
android:textColor="@color/black" />
<!--android:scaleType="fitXY"-->
<ImageView android:id="@+id/list_image"
android:layout_height="180dp"
android:layout_margin="2dp"
android:layout_weight="0.04"
android:layout_width="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:src="@mipmap/navigation_refresh"/>
<TextView android:id="@+id/description"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
android:textSize="@dimen/description3"
android:textColor="@color/black" />
</LinearLayout>
现在,我有一个带有列表视图的片段。该片段的作用与教程完全相同(显示 8 张卡片)。我在要更改特定卡片边框颜色的片段中添加了一个按钮(在 onactivitycreated 片段中):
newpost = (Button) getActivity().findViewById(R.id.reply);
newpost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayerDrawable layers = (LayerDrawable) getActivity().getResources().getDrawable(R.drawable.feed_item);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.feedsquare));
shape.setStroke(1 ,R.color.green);
}
});
按下按钮时没有任何反应。关于我能做什么有什么想法吗?
在列表视图中显示选定的项目(行):
- 在适配器中创建一个变量 class :
private int selectedPosition = 0;
在侧适配器中创建方法:
public void setSelectedItem(int position) {
this.selectedPosition = position;
notifyDataSetChanged();
}
在 getView 方法中添加检查
if(位置== selectedPosition)
{
convertView.setBackground(context.getResources().getDrawable(R.drawable.selector_rect_black_trans_bg));
}
else convertView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
selector_rect_black_trans_bg.xml :
<solid android:color="@color/gray1" />
<stroke
android:width="2dp"
android:color="@color/gray6" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" />
在Activity中,只需要在adapter中传递position即可高亮显示。
它经过测试的代码,它会工作。
我正在研究 this tutorial 并且有这样的想法,当你按下一个按钮时,其中一张卡片的边框会改变颜色。
我查看了 this solution,但没有任何反应。
这是我所做的:
我有一个 xml 可绘制形状 (feed_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#d8d8d8" />
<corners android:radius="7dp" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:id="@+id/feedsquare">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color -->
<solid
android:color="@color/white" >
</solid>
<!-- view border color and width (I WANT TO CHANGE THIS) -->
<stroke
android:width="1dp"
android:color="@color/white" >
</stroke>
<!-- If you want to add some padding -->
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" >
</padding>
</shape>
</item>
</layer-list>
然后我的布局基本上就是每个 card/list 项的外观 (list_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:background="@drawable/feed_item"
android:id="@+id/card"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="2dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/title"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMM"
android:textSize="@dimen/name3"
android:textColor="@color/black" />
<!--android:scaleType="fitXY"-->
<ImageView android:id="@+id/list_image"
android:layout_height="180dp"
android:layout_margin="2dp"
android:layout_weight="0.04"
android:layout_width="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:src="@mipmap/navigation_refresh"/>
<TextView android:id="@+id/description"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_width="wrap_content"
android:text="MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM"
android:textSize="@dimen/description3"
android:textColor="@color/black" />
</LinearLayout>
现在,我有一个带有列表视图的片段。该片段的作用与教程完全相同(显示 8 张卡片)。我在要更改特定卡片边框颜色的片段中添加了一个按钮(在 onactivitycreated 片段中):
newpost = (Button) getActivity().findViewById(R.id.reply);
newpost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayerDrawable layers = (LayerDrawable) getActivity().getResources().getDrawable(R.drawable.feed_item);
GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.feedsquare));
shape.setStroke(1 ,R.color.green);
}
});
按下按钮时没有任何反应。关于我能做什么有什么想法吗?
在列表视图中显示选定的项目(行):
- 在适配器中创建一个变量 class :
private int selectedPosition = 0;
在侧适配器中创建方法:
public void setSelectedItem(int position) { this.selectedPosition = position; notifyDataSetChanged(); }
在 getView 方法中添加检查
if(位置== selectedPosition) { convertView.setBackground(context.getResources().getDrawable(R.drawable.selector_rect_black_trans_bg)); } else convertView.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
selector_rect_black_trans_bg.xml :
<solid android:color="@color/gray1" /> <stroke android:width="2dp" android:color="@color/gray6" /> <corners android:bottomLeftRadius="0dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="0dp" />
在Activity中,只需要在adapter中传递position即可高亮显示。 它经过测试的代码,它会工作。