复杂的代码布局

今天研究了一下全代码布局,使用了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

复杂的代码布局的相关文章

【转】一个DIV+CSS代码布局的简单导航条

原文地址:http://www.divcss5.com/shili/s731.shtml 简单的DIV CSS代码布局实现导航条 一个蓝色主题的导航条布局案例,本CSS小实例,采用DIV CSS实现.同时不用图片做背景,直接使用背景色实现,鼠标经过悬停对应栏目名称是对应背景蓝色变深. 导航条部分效果截图 一般导航条采用ul li列表布局,这里也不例外DIVCSS5实例也采用列表标签ul li+ CSS布局. 一.布局思维思考   -   TOP 在实际DIV+CSS布局项目中,一般不会只使用一次

OC iOS开发 代码布局

代码布局抛弃storyboard,用代码生成界面,它的优劣不谈 首先在项目设置中,更改应用的“入口” 不选main,清空它 然后在AppDelegate.m中,更改(添加内容),别忘了import 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 // Override point for customization

002.[python学习]python编码规范pep8学习——PEP8第一部分代码布局

关于PEP8的详细说明可以参考官方原文:http://legacy.python.org/dev/peps/pep-0008/ 我参考官方文档及其他文章,摘出相关内容而得此文章,具体参考其他文章见文中最后参考资料处. 当想要让自己所写的代码为更多人使用.交流学习时,不能写出只有机器认识的代码,而是对于人而言具有良好的可读性,此时就需要遵从一个公共的约束来规范自己的代码,那么<Style Guide for Python Code(PEP8)>是个很好的选择. 首先PEP8中声明,有以下有理由忽

IOS开发UI篇--UITableView的自定义布局==纯代码布局

UITableView中除了利用系统的UItableViewCell不能完成需求进行布局时,还可以进行自定义布局: 自定义布局分为两类:(1)利用代码进行创建 (2)利用xib进行实现: 下面对利用代码进行创建分析: 应用场景:像微博,等列表数据展示(由于微博的每个单元格的数据大小不一致,所以得计算每个单元格的大小) 分析:前提是获取列表数据,然后建立每个单元格的模型(建立单元格模型应继承UITableViewCell)复写 - (id)initWithStyle:(UITableViewCel

代码布局relativeLayout

后台布局   在ANDROID 开发中有时候我们需要在后台添加布局文件这里我们来说一下后台添加RelativeLayout文件的方式: RelativeLayout,顾名思义,就是以“相对”位置/对齐为基础的布局方式.android.widget.RelativeLayout 有个 继承自android.view.ViewGroup.LayoutParams 的内嵌类 LayoutParams,使用这个类的实例调用 RelativeLayout.addView 就可以实现“相对布局”. 首先我们

Android原理——动态代码布局

动态代码布局 如何添加代码布局 代码布局注意的问题 代码布局和XML布局的性能比较 如何添加代码布局 for example -- 简单布局LinearLayout LinearLayout llayout = new LinearLayout(mContext); llayout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParam

TabLayout 代码布局以及一些小问题

TabLayout是2015发布在Design Support Library中的一个组件. 最近用TabLayout进行代码布局的时候发现一个不大不小的问题. 首先介绍如何对TabLayout进行代码布局: 首先要有一个FragmentActivity: package com.example.testapp; import android.annotation.TargetApi; import android.content.Context; import android.graphics

Object-C iOS纯代码布局 一堆代码可以放这里!

前言: 最近写的文章都是创业类,好吧,今天好好写写技术类的文章! 不过分享的不是IOS相关的文章,毕竟这几天在速成IOS,看的是objective-c,由于速成的很快,好累! 好在现在基本已经入了点门道了,这才看的懂新人的代码,才能提前感受代码危机. 对于IOS的速成口诀,回头再分享,今天先分享一个简单的抽象封装! 1:先看看问题 说明:通常对于界面布局,有编写代码.Xib.Storyboard三种方式,而我看到新人写的,是通过代码布局的. 看到的代码是这样的: A:编写UI的代码已分离到新的类

浅谈IOS的代码布局&lt;一&gt;

在我们需要开发一个ios应用的时候,我们通常会首先考虑到应用的开发框架.ios开发基于纯代码布局和xib,storyboard布局有差异,这里只讨论到纯代码布局.因为纯代码的布局在代码迁移中效率更高,重用更加方便.      一般一个ios应用会分为logic层(处理逻辑的类),UI层(处理界面的类),网络层(处理网络接口),基础层(一些第三方控件或者是自己编写的工具类),属性基类(全局性的通用属性类或者是logic层面用到的特定属性类).其中,基础层和网络层的代码是可以在每个项目中迁移使用的,