Android ListView圓角

來源:互聯網
上載者:User

標籤:find   extc   pen   ret   san   sts   list   lis   comm   

首先來看看ListView 相關基本屬性

1、單擊列表後,列表的背景變成黑色了。

可通過指定android:cacheColorHint的屬性來放變它,將它指定為透明。

使用以下的屬性值:
    android:cacheColorHint="#000000" 就可以。
2、去除清單項目中間的切割線:android:divider="#00000000",這裡的值也能夠指向一個drawable圖片對象(android:divider="@drawable/list_line")。假設使用了圖片高度大於系統的像素的話,能夠自己設定一個高度。

android:dividerHight="10px"
3、listview在拖動時。listview的背景變成黑色。用這個可解決:android:scrollingCache="false"
4、listview的上邊和下邊有黑色的陰影。用這個可解決:android:fadingEdge="none"
5、listview右邊的滑動條覆蓋清單項目的內容。用這個可解決:android:scrollbarStyle="outsideInset"
6、改動listvew右邊的滑動條與清單項目之間的距離。

用這個可解決:android:paddingRight="10dip"。能夠依據須要進行改動。
7、改動右邊的滑動條顯示的顏色。用這個可解決:
    android:scrollbarTrackVertical="@drawable/scrollbar_vertical_track"
    android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"  
當中scrollbar_vertical_track和scrollbar_vertical_thumb是滑動條的XML設定檔,在API中有,可依據自己的須要改動一下開始顏色和結束顏色就可以。

詳細請參考這篇文章 Android ListView 屬性

以下來看看ListView 圓角實現,本文主要亮點是資料來源能夠靈活設定。不用在xml上寫死了。

1.在drawable檔案下建立

  list_bottom_selector.xml

 

<?

xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"><shape> <solid android:color="#ADFF2F" /> <corners android:bottomLeftRadius="4dp" android:bottomRightRadius="4dp" /> </shape></item> <item><shape> <solid android:color="@color/white" /> <corners android:bottomLeftRadius="4dp" android:bottomRightRadius="4dp" /> </shape></item></selector>

如法炮製 建立幾個xml

list_rect_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"><shape>            <solid android:color="#ADFF2F" />        </shape></item>    <item><shape>            <solid android:color="@color/white" />        </shape></item></selector>

list_round_selector.xml

<?xml version="1.0" encoding="utf-8"?>    <selector xmlns:android="http://schemas.android.com/apk/res/android">                <item android:state_pressed="true"><shape>                  <solid android:color="#ADFF2F" />                  <corners android:bottomLeftRadius="4dp" android:bottomRightRadius="4dp"                     android:topLeftRadius="4dp" android:topRightRadius="4dp"/>              </shape></item>          <item><shape>                  <solid android:color="@color/white" />                        <corners android:bottomLeftRadius="4dp" android:bottomRightRadius="4dp"                     android:topLeftRadius="4dp" android:topRightRadius="4dp"/>              </shape></item>            </selector>  

list_top_selector.xml

<?xml version="1.0" encoding="utf-8"?

> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"><shape> <solid android:color="#ADFF2F" /> <corners android:topLeftRadius="4dp" android:topRightRadius="4dp" /> </shape></item> <item><shape> <solid android:color="@color/white" /> <corners android:topLeftRadius="4dp" android:topRightRadius="4dp" /> </shape></item> </selector>


接下來我們在layout建立一個item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:paddingRight="5dp"    >    <TextView         android:id="@+id/text"        android:layout_width="fill_parent"        android:layout_height="40dp"       android:textSize="16sp"       android:paddingLeft="15dp"       android:textColor="#000000"        android:gravity="left|center_vertical"        />        <ImageView         android:src="@drawable/common_left"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_alignParentRight="true"        /></RelativeLayout>

後建立一個activity_main.xml 來作為我們程式的首頁面。也就是我們要顯示ListView,布局也非常easy

例如以下:

<?xml version="1.0" encoding="utf-8"?

><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="15dp" > <ListView android:id="@+id/list1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/round_bg" android:cacheColorHint="#00000000" android:divider="@color/list_divider" android:dividerHeight="0.1dp" android:listSelector="#00000000" > </ListView> <ListView android:id="@+id/list2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@drawable/round_bg" android:cacheColorHint="#00000000" android:divider="@color/list_divider" android:dividerHeight="0.1dp" android:listSelector="#00000000" > </ListView> <ListView android:id="@+id/list3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:background="@drawable/round_bg" android:cacheColorHint="#00000000" android:divider="@color/list_divider" android:dividerHeight="1px" android:listSelector="@color/transparent" > </ListView> </LinearLayout>


然後建立一個MainActivity

public class MainActivity extends Activity {private ListView list1;private ListView list2;private ListView list3;String array1[] = {"設定"};String array2[] = {"朋友圈","我的空間"};String array3[] = {"我的錢包","我的收藏","我的相冊","關聯"};@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}    private void initView()    {list1 = (ListView) findViewById(R.id.list1);list2 = (ListView) findViewById(R.id.list2);list3 = (ListView) findViewById(R.id.list3);list1.setAdapter(new MyListAdapter(array1,this));list2.setAdapter(new MyListAdapter(array2,this));list3.setAdapter(new MyListAdapter(array3,this));    }}


後面我們自己定義一個MyListAdapter 繼承BaseAdapter

public class MyListAdapter extends BaseAdapter {private LayoutInflater inflater;private String[] array;private Context mContext;public MyListAdapter(String[] array,Context context) {inflater = LayoutInflater.from(context);this.array = array;this.mContext =context; }@Overridepublic int getCount() {// TODO Auto-generated method stubreturn array.length;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn array[position];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubconvertView = inflater.inflate(R.layout.item, null);TextView tv = (TextView) convertView.findViewById(R.id.text);tv.setText(array[position]);if (array.length == 1) {setBackgroundDrawable(convertView,R.drawable.list_round_selector);} else if (array.length == 2) {if (position == 0) {setBackgroundDrawable(convertView,R.drawable.list_top_selector);} else if (position == array.length - 1) {setBackgroundDrawable(convertView,R.drawable.list_bottom_selector);}} else {if (position == 0) {setBackgroundDrawable(convertView,R.drawable.list_top_selector);} else if (position == array.length - 1) {setBackgroundDrawable(convertView,R.drawable.list_bottom_selector);} else {setBackgroundDrawable(convertView,R.drawable.list_rect_selector);}}return convertView;}private void setBackgroundDrawable(View view, int resID) {view.setBackgroundDrawable(mContext.getResources().getDrawable(resID));}}



   

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