【发布时间】:2014-04-01 01:25:12
【问题描述】:
我正在尝试使用可扩展列表的父级设置一个特定视图的颜色,但是当我这样做时,我发现子视图中的颜色也受到影响,这很奇怪。
将视图从白色更改为黑色似乎可以解决问题,而且它似乎只出现在某些手机上,它真的开始让我发疯,但我找不到任何遇到同样问题的人。
这是我扩展 BaseExpandableListAdapter 的自定义类。
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(SUM_HOST, parent, false);
}
View statView = (View)v.findViewById(R.id.statViewABCDE);
int caseTest = hostParents.get(groupPosition).getState();
switch (caseTest) {
case Constants.STATE_OK:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_up_state));
break;
case Constants.STATE_DOWN:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_down_state));
break;
case Constants.STATE_UNKNOWN:
statView.setBackgroundColor(ctx.getResources().getColor(R.color.host_unreachable_state));
break;
}
return v;
}
这是我的父级 XML 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp" >
<LinearLayout
android:id="@+id/sumHostLay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/defViewMargin"
android:layout_marginRight="@dimen/defViewMargin"
android:layout_marginTop="@dimen/defViewMargin"
android:background="@drawable/rounded_corners_background"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="@dimen/defViewPadding" >
<View
android:id="@+id/statViewABCDE"
android:layout_width="3dip"
android:layout_height="fill_parent"
android:background="@android:color/black" />
<TextView
android:id="@+id/sumHostTitleAndOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="@string/hostrow_hostname"
android:textSize="13sp" />
</LinearLayout>
这是我给孩子的 XML。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="0dp" >
<LinearLayout
android:id="@+id/asdasd"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="@dimen/defViewMargin"
android:orientation="vertical" >
<View
android:id="@+id/line1"
android:layout_width="1dip"
android:layout_height="fill_parent"
android:layout_margin="3dp"
android:background="@android:color/white" />
</LinearLayout>
<LinearLayout
android:id="@+id/line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<View
android:id="@+id/line4"
android:layout_width="20dp"
android:layout_height="1dp"
android:background="@android:color/white" />
</LinearLayout>
理论上我只是想在父 XML 中设置视图,即 statViewABCDE 但是我发现背景颜色也在子视图中发生变化。特别是“line1”和“line4”正在改变。
编辑:
将线条设置为不同的颜色(即一组为黑色,另一组为白色,似乎有效,尽管我不确定为什么。)
在运行 Android 4.1.2 的手机上测试时发现此问题。
【问题讨论】:
标签: java android colors background expandablelistview