安卓學習第15課——ExpandableListView

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   io   ar   2014   

AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
這句話的意思是 建立一個布局(LayoutParams)的執行個體 lp。
AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64) 指定了該布局的寬和高;
-1代表LayoutParams.MATCH_PARENT,即該布局的尺寸將填滿它的父控制項;
-2代表LayoutParams.WRAP_CONTENT,即該布局的尺寸將為其自身內容的尺寸;
下面是BaseExpandableListAdapter的用法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"   android:orientation="vertical">   <ExpandableListView    android:id="@+id/list"    android:layout_width="match_parent"     android:layout_height="wrap_content"     android:childIndicator="@drawable/ic_launcher"    /></LinearLayout>
package com.example.expandablelistview;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.content.Context;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.os.Build;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //建立一個BaseExpandableListAdapter對象        ExpandableListAdapter adapter=new BaseExpandableListAdapter(){            int[] logos=new int[]{                    R.drawable.p,R.drawable.z,R.drawable.t            };            private String[] armTypes=new String[]{                "神族兵種","蟲族兵種","人族兵種"                };            private String[][] arms=new String[][]{                    {"狂戰士","龍騎士","黑暗聖堂","點兵"},                    {"小狗","刺蛇","飛龍","自爆飛機"},                    {"機槍兵","護士mm","幽靈"}            };            @Override            public int getGroupCount() {                return arms.length;            }            @Override            public int getChildrenCount(int groupPosition) {                                return arms[groupPosition].length;            }            @Override            public Object getGroup(int groupPosition) {                return armTypes[groupPosition];            }            @Override            public Object getChild(int groupPosition, int childPosition) {                return arms[groupPosition][childPosition];            }            @Override            public long getGroupId(int groupPosition) {                return groupPosition;            }            @Override            public long getChildId(int groupPosition, int childPosition) {                return childPosition;            }            @Override            public boolean hasStableIds() {                return true;            }            @Override            public View getGroupView(int groupPosition, boolean isExpanded,                    View convertView, ViewGroup parent) {                LinearLayout ll=new LinearLayout(MainActivity.this);                ll.setOrientation(0);                ImageView logo=new ImageView(MainActivity.this);                logo.setImageResource(logos[groupPosition]);                ll.addView(logo);                TextView textView=getTextView();                textView.setText(getGroup(groupPosition).toString());                ll.addView(textView);                return ll;            }            private TextView getTextView(){                AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);                TextView textView=new TextView(MainActivity.this);                textView.setLayoutParams(lp);                textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);                textView.setPadding(36, 0,0,0);                textView.setTextSize(20);                return textView;            }            @Override            public View getChildView(int groupPosition, int childPosition,                    boolean isLastChild, View convertView, ViewGroup parent) {                TextView textView=getTextView();                textView.setText(getChild(groupPosition,childPosition).toString());                return textView;            }            @Override            public boolean isChildSelectable(int groupPosition,                    int childPosition) {                return true;            }                    };        ExpandableListView expandableView=(ExpandableListView) findViewById(R.id.list);    expandableView.setAdapter(adapter);    }}

安卓學習第15課——ExpandableListView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.