gpt4 book ai didi

android - 根据特殊情况更改 ListView 布局背景颜色

转载 作者:太空狗 更新时间:2023-10-29 15:59:31 26 4
gpt4 key购买 nike

我在 Fragment 中创建了一个 ListView 并使用 SimpleAdapter 来设置布局和值。

现在,我想根据为 ListView 设置的文本更改在 ListView 中使用的布局中 LinearLayout 的背景。< br/>像这样:

Something Like This

这是我的布局 XML。
我想用 list id 改变 LinearLayout 的颜色。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="@drawable/layout_shadow"
android:layout_height="207dp">

<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:layout_height="193dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:id="@+id/list">

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:text="magnitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fValue"
android:textStyle="bold"
android:textColor="@android:color/white"
android:textSize="20sp"
android:layout_margin="5dp" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:text="region"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sValue"
android:textColor="@android:color/white" />
</LinearLayout>

<ImageView
android:layout_width="match_parent"
app:srcCompat="@drawable/no_image"
android:id="@+id/mImage"
android:layout_height="143dp" />
</LinearLayout>
</RelativeLayout>

最佳答案

  1. 您需要通过扩展 SimpleAdapter 类来自定义 SimpleAdapter 并覆盖所有必需的方法。

  2. 在getView()方法下需要编写如下代码

  3. 确保为所有设置 ID,以便您可以轻松访问 java 代码。

  4. 现在从 TextView 的文本(如“绿色”)中获取文本(颜色)并设置 LinearLayout 的背景颜色(如 LinearLayout_Object.setBackgroundColor(Color.parseColor("green"));)

注意:但是要从数组/列表中获取对象/值,您还需要使用“Position”

关于android - 根据特殊情况更改 ListView 布局背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41198180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com