如何在Android中使自定义ListView透明?

时间:2013-06-21 10:10:54

标签: android android-listview background-color

您好我已经创建了自定义列表视图,我想让单元格bg透明/ 我的列表视图:

public class FlingListView extends ListView {

    public FlingListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public FlingListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public FlingListView(Context context) {
        super(context);
    }


    private GestureDetector detector; //this is my detector

    public void setDetector(GestureDetector detector){
        this.detector = detector;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        detector.onTouchEvent(ev);
        return super.onTouchEvent(ev);
    }

}

的xml:

<sk.zp.fling.listView.FlingListView
        android:id="@+id/list_mailings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout1"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:listSelector="@drawable/list_selector"
        android:overScrollMode="always" >
    </sk.zp.fling.listView.FlingListView>

选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/zp_green" android:state_pressed="true"/>
    <item android:drawable="@color/zp_green" android:state_pressed="false" android:state_selected="true"/>

</selector>

列出CellView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="86dp"
    android:layout_gravity="left"
    android:background="@android:color/transparent"
    android:gravity="left" >

    <TextView
        android:id="@+id/cell_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="15dp"
        android:background="@android:color/transparent"
        android:layout_marginRight="15dp"
        android:layout_marginTop="3dp"
        android:gravity="center_vertical"
        android:text="25.06.1997" />

    <ImageView
        android:id="@+id/cell_logo"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_marginBottom="2dp"
        android:background="@android:color/transparent"
        android:layout_marginRight="15dp"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/cell_date"
        android:adjustViewBounds="true"
        android:layoutDirection="ltr"
        android:scaleType="fitCenter"
        android:src="@drawable/spp"
        android:textAlignment="viewStart"
        android:textDirection="ltr" />

    <TextView
        android:id="@+id/cell_company_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/transparent"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:layout_toLeftOf="@+id/cell_arrow"
        android:layout_toRightOf="@+id/cell_logo"
        android:gravity="center_vertical"
        android:lines="2"
        android:maxLines="2"
        android:text="Dodavatel a balab bluba lsnhso"
        android:textSize="14sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/cell_arrow"
        android:layout_width="36dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:layout_marginBottom="2dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="2dp"
        android:paddingLeft="15dp"
        android:src="@drawable/arrow_custom" />

</RelativeLayout>

我在哪里做错了?因为它总是白色的bg。如果我改变任何其他颜色而不是透明它的工作....所以它可能是黑色,棕红色但不透明:(

请帮助

what it looks like: How the other screens look like

2 个答案:

答案 0 :(得分:2)

这可能有效......

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


 <item android:state_pressed="false"
            android:state_selected="false"
            android:drawable="@color/Transparent" />


    <item android:drawable="@color/zp_green" android:state_pressed="true"/>
    <item android:drawable="@color/zp_green" android:state_pressed="false" android:state_selected="true"/>

</selector>

答案 1 :(得分:1)

只删除布局和视图中的所有透明 用这个。对我来说几乎是同样的要求。

clientInfoAdapter= new ClientInfoAdapter(this,R.layout.listview_item_row,listOfClients);
        ListView clientListView = (ListView)findViewById(R.id.common_custlist_lst);
        clientListView.setCacheColorHint(Color.TRANSPARENT);
        //clientListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        clientListView.setAdapter(clientInfoAdapter);