复杂的代码布局

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ConstraintLayout是一种相对定位的布局方式,它允许您在屏幕上放置视图并将它们相对于其他视图的位置和大小进行定义。在Java代码中使用ConstraintLayout,您可以使用以下步骤: 1. 在XML布局文件中定义ConstraintLayout容器。 2. 使用LayoutParams类创建视图对象并将其添加到ConstraintLayout容器中。 3. 对于每个视图,使用ConstraintSet类定义约束条件。 4. 使用applyTo()方法将约束条件应用于ConstraintLayout容器。 以下是一个简单的例子,展示如何使用Java代码创建一个包含两个文本视图和一个按钮的布局: ``` // 创建ConstraintLayout容器 ConstraintLayout layout = new ConstraintLayout(this); layout.setLayoutParams(new ConstraintLayout.LayoutParams( ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)); // 创建文本视图1 TextView textView1 = new TextView(this); textView1.setText("Hello"); textView1.setId(View.generateViewId()); layout.addView(textView1); // 创建文本视图2 TextView textView2 = new TextView(this); textView2.setText("World"); textView2.setId(View.generateViewId()); layout.addView(textView2); // 创建按钮 Button button = new Button(this); button.setText("Click me"); button.setId(View.generateViewId()); layout.addView(button); // 定义约束条件 ConstraintSet set = new ConstraintSet(); set.clone(layout); set.connect(textView1.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 16); set.connect(textView1.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 16); set.connect(textView2.getId(), ConstraintSet.TOP, textView1.getId(), ConstraintSet.BOTTOM, 16); set.connect(textView2.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 16); set.connect(button.getId(), ConstraintSet.TOP, textView2.getId(), ConstraintSet.BOTTOM, 16); set.connect(button.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 16); set.connect(button.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 16); // 应用约束条件 set.applyTo(layout); // 将布局添加到视图层次结构中 setContentView(layout); ``` 这个例子创建了一个简单的垂直布局,其中文本视图1位于顶部,文本视图2位于其下方,按钮位于文本视图2下方。每个视图都定义了与其他视图的约束条件,使它们正确地定位和调整大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值