close

能夠在ListView想要更改每個item的高度或是要更改字體大小


先自定義自己ListAdapter

public class ListAdapter extends BaseAdapter{
    Context context;
    LayoutInflater inflater;
    String[] str;
    public ListAdapter(Context context, String[] arraylist) {
        this.context = context;
        str = arraylist;
    }
    @Override
    public int getCount() {
        return str.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView text;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.list_item, parent, false);
        text = (TextView) itemView.findViewById(R.id.text1);
        text.setText(str[position]);

        return itemView;
    }
}

記得這個是每個ListView裡面的其中一個item的layout

所以在拉layout的時候是針對你想要呈現的那一格長怎樣去拉

 

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <TextView
        android:textSize="10pt"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:id="@+id/text1"
        android:textColor="#000000"/>
    
</RelativeLayout>

如果你想要更改每一格的高度的話

修改layout_height這個就可以

 

然後要做的就是你在你想要呈現的頁面的layout拉個Listview

然後把上面的Adpter指定到你的ListView就可以了

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="lynn.example.com.javalearn.MainActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#ffffff"         
        android:cacheColorHint="#aaaaaa"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"></ListView>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

 

如果要改變分隔線的顏色的話

要改ListView 的 divider這個

若是想要讓使用者點下去會改變顏色的話

則是改CacheColorHint這個

 

MainActivity.java

private String TAG = "MainActivity";
private String[] list = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
private ListView listView;
private ListAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView)findViewById(R.id.listview);
    listAdapter = new ListAdapter(this,list);
    listView.setAdapter(listAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "Your choice is " + list[position], Toast.LENGTH_SHORT).show();
        }
    });
}

 

如果想要直接看整包的程式碼可以從這裡下載喔~~

https://github.com/YuLingHung/SimpleListAdpter.git

 

 

 

arrow
arrow
    文章標籤
    Android Listview JAVA
    全站熱搜
    創作者介紹
    創作者 小小工程師 的頭像
    小小工程師

    理工女孩

    小小工程師 發表在 痞客邦 留言(0) 人氣()