【发布时间】:2017-05-03 01:32:06
【问题描述】:
我在Fragment 中创建ListView 并使用SimpleAdapter 设置布局和值。
现在我想根据为ListView 设置的文本更改在ListView 中使用的布局中LinearLayout 的背景。
像这样的:
这是我的布局 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>
【问题讨论】:
-
在您的适配器中,检查位置并更改布局颜色。比如,如果位置==0 Color.RED,位置==1 COLOR.BLUE
-
@Divyesh 谢谢,但我想根据 textView 中的文本进行更改。但是你所说的改变它取决于位置。
-
然后使用每个位置的文本值进行更改。
标签: android android-layout listview android-studio