java - 更改列表项背景边框的颜色

标签 java android xml android-layout

我正在经历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>

然后我有一个布局,基本上就是每个卡片/列表项的外观(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>

现在,我有一个带有 ListView 的 fragment 。该 fragment 的作用与教程完全相同(显示 8 张卡片)。我在 fragment 中添加了一个按钮,我想更改特定的卡片边框颜色(在 onactivitycreated 的 fragment 中):

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);

    }
});

当我按下按钮时没有任何反应。关于我能做什么有什么想法吗?

最佳答案

在 ListView 中显示所选项目(行):

  1. 在适配器类中创建变量:

私有(private) int selectedPosition = 0;

  • 在侧面适配器中创建方法:

     public void setSelectedItem(int position) {
    this.selectedPosition = position;
    notifyDataSetChanged();
    } 
    
  • 在 getView 方法中添加检查

    if(位置==所选位置) { ConvertView.setBackground(context.getResources().getDrawable(R.drawable.selector_rect_black_trans_bg)); } 否则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 中,只需传递适配器中的位置即可突出显示。 其经过测试的代码可以正常工作。

    关于java - 更改列表项背景边框的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389285/

    相关文章:

    javascript - 是否有 HQMF 到 QRDA cat 3 计算的简单示例?

    java - 通过 XML DOM 中的字符串参数检索内容

    java - android java onClick 无法执行 Activity 的方法

    compatibility - JDK 1.6 与 JDK 1.5 兼容吗?

    Android TabHost 选项卡在使用硬件键盘时窃取焦点

    Java NullPointerException 二维数组在构造函数中初始化,但方法拒绝确认

    android - 如何将全局变量从一个类获取到另一个类?

    android - 用于在 Android 中接听电话的 slider 按钮

    java - 创建一个HashMap,固定Key对应一个HashSet。出发点

    java - Playframework 2.5.0 : Unable to serve server-side generated zip file