今天研究了一下全代码布局,使用了TextView、EditText、ScrollView、Spinner、ArrayAdapter、LinearLayout、RelativeLayout、ExpandableListView、BaseExpandableListAdapter等控件,其中ExpandableListView是属于复杂的控件。
效果图如下:
关键代码如下:
package com.example.mytest; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.text.InputFilter; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.BaseExpandableListAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.ScrollView; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class TestExpandableListViewActivity extends Activity { private ExpandableListView exListView; private List<String> groupArray = new ArrayList<String>(); private List<ArrayList<String>> childArray = new ArrayList<ArrayList<String>>(); private MyAdapter myAdapter; String[] spinnerDatas = new String[]{"第一个选项","第二个选项","第三个选项"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); initData(); } private void initData() { groupArray.add("好友"); groupArray.add("同学"); ArrayList<String> ay1 = new ArrayList<String>(); ay1.add("张三"); childArray.add(ay1); ArrayList<String> ay2 = new ArrayList<String>(); ay2.add("万物2"); childArray.add(ay2); myAdapter = new MyAdapter(); exListView.setAdapter(myAdapter); exListView.setGroupIndicator(null);//去掉箭头 //展开 for(int i = 0; i < myAdapter.getGroupCount(); i++){ exListView.expandGroup(i); } } /** * 初始化界面 */ private void initView() { ScrollView main = new ScrollView(this); main.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); main.setBackgroundColor(Color.WHITE); //根布局参数 LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT); layoutParamsRoot.gravity = Gravity.CENTER; //根布局 LinearLayout layoutRoot = new LinearLayout(this); layoutRoot.setLayoutParams(layoutParamsRoot); layoutRoot.setOrientation(LinearLayout.VERTICAL); //上边距(dp值) int margin = dip2px(this, 5); //添加一个textview LinearLayout.LayoutParams layoutParamsTextInfo = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,dip2px(this, 50)); TextView textInfo = new TextView(TestExpandableListViewActivity.this); textInfo.setGravity(Gravity.CENTER_VERTICAL); textInfo.setTextSize(18); textInfo.setBackgroundColor(Color.CYAN); textInfo.setPadding(margin, 0, 0, 0); textInfo.setText("什么配电系统参数"); layoutRoot.addView(textInfo,layoutParamsTextInfo); //添加输入项 LinearLayout.LayoutParams layoutParamsEdittext = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,dip2px(this, 50)); EditText editInfo = new EditText(TestExpandableListViewActivity.this); editInfo.setHint("请输入文字内容"); //设置可输入的最大长度 InputFilter[] filters = {new InputFilter.LengthFilter(5)}; editInfo.setFilters(filters); editInfo.setTextSize(18); editInfo.setPadding(dip2px(this, 8), 0, 0, 0); layoutRoot.addView(editInfo,layoutParamsEdittext); LinearLayout.LayoutParams layoutParamsTextInfo2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,dip2px(this, 50)); TextView textInfo2 = new TextView(TestExpandableListViewActivity.this); textInfo2.setGravity(Gravity.CENTER_VERTICAL); textInfo2.setTextSize(18); textInfo2.setBackgroundColor(Color.CYAN); textInfo.setPadding(margin, 0, 0, 0); textInfo2.setText("选择配电系统参数"); layoutRoot.addView(textInfo2,layoutParamsTextInfo2); //添加下来选择框 Spinner sp = new Spinner(this); LinearLayout.LayoutParams layoutParamsSp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,dip2px(this, 50)); ArrayAdapter<String> myadapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerDatas ); myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(myadapter); sp.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) { Toast.makeText(TestExpandableListViewActivity.this, spinnerDatas[position], 1).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); layoutRoot.addView(sp,layoutParamsSp); //添加expandablelistview LinearLayout.LayoutParams layoutParamsExpand = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); exListView = new MyExpandableListView(this); layoutRoot.addView(exListView, layoutParamsExpand); //添加点击上一页下一页按钮 LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,dip2px(this, 70)); LinearLayout layoutBottom = new LinearLayout(this); layoutBottom.setLayoutParams(layoutParamsBottom); layoutBottom.setOrientation(LinearLayout.HORIZONTAL); layoutBottom.setGravity(Gravity.CENTER); Button leftBtn = new Button(this); leftBtn.setText("上一步"); Button rightBtn = new Button(this); rightBtn.setText("下一步"); layoutBottom.addView(leftBtn); layoutBottom.addView(rightBtn); layoutRoot.addView(layoutBottom,layoutParamsBottom); //将界面加载进去 main.addView(layoutRoot); setContentView(main); exListView.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView arg0, View arg1, int position, long arg3) { //true无法关闭,false可以点击打开关闭 return true; } }); exListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView arg0, View arg1, int groupPosition, int childPosition, long arg4) { return false; } }); } class MyAdapter extends BaseExpandableListAdapter { @Override public Object getChild(int groupPosition, int childPosition) { return childArray.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String string = childArray.get(groupPosition).get(childPosition); return getChildView(string,groupPosition,childPosition); } @Override public int getChildrenCount(int groupPosition) { return childArray.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupArray.get(groupPosition); } @Override public int getGroupCount() { return groupArray.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String string = groupArray.get(groupPosition); return getParentView(string,groupPosition); } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int arg0, int arg1) { return true; } public RelativeLayout getParentView(final String string,final int position) { AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, dip2px(TestExpandableListViewActivity.this, 60)); RelativeLayout relaLayout = new RelativeLayout(TestExpandableListViewActivity.this); relaLayout.setLayoutParams(layoutParams); relaLayout.setBackgroundColor(Color.CYAN); RelativeLayout.LayoutParams paramsImageText = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); paramsImageText.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); paramsImageText.setMargins(dip2px(TestExpandableListViewActivity.this, 15), 0, 0, 0); //初始化textInfo TextView textInfo = new TextView(TestExpandableListViewActivity.this); textInfo.setGravity(Gravity.CENTER_HORIZONTAL); textInfo.setTextSize(18); textInfo.setText(string); //初始化右边add按钮 RelativeLayout.LayoutParams paramsImageAdd = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); paramsImageAdd.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); paramsImageAdd.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); paramsImageAdd.setMargins(0, 0, dip2px(TestExpandableListViewActivity.this, 5), 0); paramsImageAdd.width = dip2px(TestExpandableListViewActivity.this, 20); paramsImageAdd.height = dip2px(TestExpandableListViewActivity.this, 20); //初始化右边add按钮 ImageView imageAdd = new ImageView(TestExpandableListViewActivity.this); imageAdd.setScaleType(ScaleType.FIT_XY); imageAdd.setAdjustViewBounds(true); imageAdd.setImageResource(R.drawable.add); imageAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(TestExpandableListViewActivity.this, "添加", 1).show(); childArray.get(position).add("new add"); notifyDataSetChanged(); } }); relaLayout.addView(textInfo, paramsImageText); relaLayout.addView(imageAdd, paramsImageAdd); return relaLayout; } public RelativeLayout getChildView(final String string,final int GroupPosition,final int childPosition) { AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, dip2px(TestExpandableListViewActivity.this, 60)); RelativeLayout relaLayout = new RelativeLayout(TestExpandableListViewActivity.this); relaLayout.setLayoutParams(layoutParams); RelativeLayout.LayoutParams paramsImageText = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); paramsImageText.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); paramsImageText.setMargins(dip2px(TestExpandableListViewActivity.this, 25), 0, 0, 0); //初始化textInfo TextView textInfo = new TextView(TestExpandableListViewActivity.this); textInfo.setGravity(Gravity.CENTER_HORIZONTAL); textInfo.setTextSize(18); textInfo.setText(string); textInfo.setId(100000001); //初始化右边delete按钮 RelativeLayout.LayoutParams paramsImageDel = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); paramsImageDel.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); paramsImageDel.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); paramsImageDel.setMargins(0, 0, dip2px(TestExpandableListViewActivity.this, 5), 0); paramsImageDel.width = dip2px(TestExpandableListViewActivity.this, 20); paramsImageDel.height = dip2px(TestExpandableListViewActivity.this, 20); //初始化右边删除按钮 ImageView imageAdd = new ImageView(TestExpandableListViewActivity.this); imageAdd.setScaleType(ScaleType.FIT_XY); imageAdd.setAdjustViewBounds(true); imageAdd.setImageResource(R.drawable.delete); imageAdd.setId(100000002); imageAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(TestExpandableListViewActivity.this, "删除", 1).show(); childArray.get(GroupPosition).remove(childPosition); notifyDataSetChanged(); } }); //添加edittext RelativeLayout.LayoutParams paramsEdit = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); paramsEdit.addRule(RelativeLayout.LEFT_OF, 100000002); paramsEdit.addRule(RelativeLayout.RIGHT_OF, 100000001); paramsEdit.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); paramsEdit.setMargins(dip2px(TestExpandableListViewActivity.this, 5), 0, 0, 0); EditText editInfo = new EditText(TestExpandableListViewActivity.this); editInfo.setHint("请输入文字内容"); //设置可输入的最大长度 InputFilter[] filters = {new InputFilter.LengthFilter(5)}; editInfo.setFilters(filters); editInfo.setTextSize(18); editInfo.setPadding(dip2px(TestExpandableListViewActivity.this, 8), 0, 0, 0); relaLayout.addView(editInfo, paramsEdit); relaLayout.addView(textInfo, paramsImageText); relaLayout.addView(imageAdd, paramsImageDel); return relaLayout; } } /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }
源码下载地址:http://download.csdn.net/detail/xiaoyi848699/8602437
时间: 2024-10-06 00:29:27