簡體   English   中英

更改列表項背景邊框的顏色

[英]Changing color of the border of the background of a list item

我正在遍歷本教程 ,並想到要做到這一點,因此當您按下按鈕時,其中一張卡片的邊框會更改顏色。

我看着這個解決方案 ,但是什么也沒有發生。

這是我所做的:

我在可繪制的圖形中有一個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>

現在,我有一個帶有列表視圖的片段。 該片段的功能與本教程的功能完全相同(顯示8張卡)。 我在片段上添加了一個按鈕,以更改特定的卡片邊框顏色(在片段中創建了onactivity):

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

    }
});

當我按下按鈕時什么也沒發生。 關於我能做什么的任何想法?

要在列表視圖中顯示選定的項目(行):

  1. 在適配器類中創建一個變量:

private int selectedPosition = 0;

  1. 在側面適配器中創建方法:

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

    如果(位置== selectedPosition){convertView.setBackground(context.getResources()。getDrawable(R.drawable.selector_rect_black_trans_bg));; } else convertView.setBackgroundColor(context.getResources()。getColor(android.R.color.transparent));

  3. 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" /> 

在“活動”中,只需要傳遞適配器中的位置即可突出顯示。 它經過測試的代碼,它將正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM