android開發之方形圓角listview

來源:互聯網
上載者:User

 我寫這篇文章受到了kiritor的專欄發表的博文Android UI控制項之ListView實現圓角效果的啟發。

先看:

 
 


首先,你得寫一個類我們命名為CornerListView


[java]
/**
 * 圓角ListView樣本
 * @Description: 圓角ListView樣本
 * @FileName: CornerListView.java 
 */ 
public class CornerListView extends ListView { 
    public CornerListView(Context context) { 
        super(context); 
    } 
 
    public CornerListView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
    } 
 
    public CornerListView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
    } 
 
    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
        switch (ev.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
                int x = (int) ev.getX(); 
                int y = (int) ev.getY(); 
                int itemnum = pointToPosition(x, y); 
 
                if (itemnum == AdapterView.INVALID_POSITION) 
                        break;                  
                else{ 
                    if(itemnum==0){ 
                        if(itemnum==(getAdapter().getCount()-1)){                                     
                            setSelector(R.drawable.<SPAN style="COLOR: #ff0000">app_list_corner_round</SPAN>); 
                        }else{ 
                            setSelector(R.drawable.<SPAN style="COLOR: #ff0000">app_list_corner_round_top</SPAN>); 
                        } 
                    }else if(itemnum==(getAdapter().getCount()-1)) 
                            setSelector(R.drawable.<SPAN style="COLOR: #ff0000">app_list_corner_round_bottom</SPAN>); 
                    else{                             
                        setSelector(R.drawable.<SPAN style="COLOR: #ff0000">app_list_corner_shape</SPAN>); 
                    } 
                } 
 
                break; 
        case MotionEvent.ACTION_UP: 
                break; 
        } 
         
        return super.onInterceptTouchEvent(ev); 
    } 

/**
 * 圓角ListView樣本
 * @Description: 圓角ListView樣本
 * @FileName: CornerListView.java
 */
public class CornerListView extends ListView {
    public CornerListView(Context context) {
        super(context);
    }

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

    public CornerListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
                int x = (int) ev.getX();
                int y = (int) ev.getY();
                int itemnum = pointToPosition(x, y);

                if (itemnum == AdapterView.INVALID_POSITION)
                        break;                
                else{
                 if(itemnum==0){
                        if(itemnum==(getAdapter().getCount()-1)){                                   
                            setSelector(R.drawable.app_list_corner_round);
                        }else{
                            setSelector(R.drawable.app_list_corner_round_top);
                        }
                 }else if(itemnum==(getAdapter().getCount()-1))
                         setSelector(R.drawable.app_list_corner_round_bottom);
                 else{                           
                     setSelector(R.drawable.app_list_corner_shape);
                 }
                }

                break;
        case MotionEvent.ACTION_UP:
                break;
        }
       
        return super.onInterceptTouchEvent(ev);
    }
}
其中,app_list_corner_round


[html] 
<SPAN style="COLOR: #333333"><?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient android:startColor="#BFEEFF"  
        android:endColor="#40B9FF"  
        android:angle="270"/> 
    <corners android:topLeftRadius="6dip" 
        android:topRightRadius="6dip" 
        android:bottomLeftRadius="6dip" 
        android:bottomRightRadius="6dip"/> 
</shape> </SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/>
    <corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"
        android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip"/>
</shape>
app_list_corner_round_top

 

[html] 
<SPAN style="COLOR: #333333"><?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient android:startColor="#BFEEFF"  
        android:endColor="#40B9FF"  
        android:angle="270"/> 
    <corners android:topLeftRadius="6dip" 
        android:topRightRadius="6dip"/> 
</shape> </SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/>
    <corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"/>
</shape>
app_list_corner_round_bottom

 

[html] 
<SPAN style="COLOR: #333333"><?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient android:startColor="#BFEEFF"  
        android:endColor="#40B9FF"  
        android:angle="270"/> 
    <corners android:bottomLeftRadius="6dip" 
        android:bottomRightRadius="6dip" /> 
</shape> </SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/>
    <corners android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip" />
</shape>
app_list_corner_shape

 

[html] 
<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <gradient android:startColor="#BFEEFF"  
        android:endColor="#40B9FF"  
        android:angle="270"/> 
</shape>  

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF"
        android:endColor="#40B9FF"
        android:angle="270"/>
</shape>
寫好了之後,就可以在你的代碼中直接像listview一樣調用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.