简体   繁体   中英

Android problems setting the background of a view in a expandable list view

I'm trying to set the color of one specific view with the parent of the expandable list, however when I do I'm finding that colors in the child views are being affected as well which is strange.

Changing the view to original view from white to black seems to solve the issue and it only seems to arise on some phones, its really starting to drive me mad, but I can't find anybody thats run into the same issue.

Heres my custom class that extends 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;

}

Here is my XML file for the parent:

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

Here is my XML for the child.

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

In theory I am just trying to set the view in the parent XML ie statViewABCDE however I am finding that the background colors are changing in the child view aswell. Specifically "line1" and "line4" are changing.

EDIT:

Setting the lines to be different colors (ie Black for one set and white for the others, seems to have worked, although I'm not sure why.)

Found this issue while testing on a phone running Android 4.1.2.

I can't find any conclusive evidence of whats going on here.

To get around this issue I set the color of the background in java of each view, this seems to have resolved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM