【问题标题】:Listview row color different for each row每行的Listview行颜色不同
【发布时间】:2018-11-27 18:38:29
【问题描述】:

您好,我正在使用带有自定义行布局的 Listview。我在 Listview 的行布局内的 CardView 中使用了 LinearLayout。我在drawable中有一些渐变形状,所以我想将它们提供给LinearLayout的背景。我怎样才能有问题地将它提供给linearlayout,所以Listview的每一行都将填充不同的渐变。 每行应该有不同的渐变背景。 这是我的 ListView 行布局

<android.support.v7.widget.CardView
            android:id="@+id/card_iq_sidebar"
            android:layout_width="50dp"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/circle_purple"
                android:orientation="vertical"></LinearLayout>
        </android.support.v7.widget.CardView>

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    在您控制自定义行的地方使用此代码,以便根据适配器位置更改背景可绘制对象。 mainlayout 是您的线性布局,但我的代码仅使用 2 个渐变。

    如果您希望每个单元格使用不同的颜色,请定义一个资源数组并使用getAdapterPosition() 作为资源数组索引。 (注意你可以很容易地越界错误)

    资源数组android using xml array resources的信息

            if(!(getAdapterPosition() % 2 == 0)){
                mainLayout.setBackgroundResource(R.drawable.bg_dashboard_row_reverse);
    
            }
            else{
                mainLayout.setBackgroundResource(R.drawable.bg_dashboard_row);
    
            }
    

    我希望你明白了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      相关资源
      最近更新 更多