[Android][设置最小(大)宽高]

#1.简介
TextView里定义的方法,据说是设置文本区域的宽高
android:maxWidth 关联方法: setMaxWidth(int) 属性说明: 设置文本区域的最大宽度
android:maxHeight 关联方法: setMaxHeight(int) 属性说明: 设置文本区域的最大高度
android:minWidth 关联方法: setMinWidth(int) 属性说明: 设置文本区域的最小宽度
android:minHeight 关联方法: setMinHeight(int) 属性说明: 设置文本区域的最小高度

View里定义的方法,只有设置视图最小宽高的方法,没有设置视图最大宽高的方法
android:minHeight 关联方法: setMinimumHeight(int) 属性说明: 设置视图最小高度
android:minWidth 关联方法: setMinimumWidth(int) 属性说明: 设置视图最小宽度

#2.代码分析

/**
 * @author LGY
 * @time 2017-12-13
 * @action 经过测试我们知道,
 * 这是TextView里定义的方法,就目前测试而言,Button是无法通过这几个方法设置小宽高的,button要设置最小宽度只能通过setMinimumWidth和setMinimumHeight
android:maxWidth 关联方法: setMaxWidth(int) 属性说明: 设置文本区域的最大宽度
android:maxHeight 关联方法: setMaxHeight(int) 属性说明: 设置文本区域的最大高度
android:minWidth 关联方法: setMinWidth(int) 属性说明: 设置文本区域的最小宽度
android:minHeight 关联方法: setMinHeight(int) 属性说明: 设置文本区域的最小高度

这两个方法是View里定义的,只有设置视图最小宽高的方法,没有设置视图最大宽高的方法
android:minHeight 关联方法: setMinimumHeight(int) 属性说明: 设置视图最小高度
android:minWidth 关联方法: setMinimumWidth(int) 属性说明: 设置视图最小宽度

 */
public class TestWidth extends Activity{

	private LinearLayout bodyLayout = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		setContentView(bodyLayout);
	}
	
	private void initView()
	{
		LinearLayout.LayoutParams params = null;
		try {
			bodyLayout = new LinearLayout(this);
			if (bodyLayout!=null) {
				bodyLayout.setOrientation(LinearLayout.VERTICAL);
				params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
				if (params!=null) {
					bodyLayout.setLayoutParams(params);
				}
				
				LinearLayout layout = new LinearLayout(this);
				if (layout!=null) {
					layout.setOrientation(LinearLayout.VERTICAL);
					params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
					if (params!=null) {
						layout.setLayoutParams(params);
					}
					layout.setMinimumHeight(100);
					layout.setBackgroundColor(Color.YELLOW);
					bodyLayout.addView(layout);
				}
				

				
				TextView textTv = new TextView(this);
				if (textTv!=null) {
					textTv.setText("测试");
//					对于TextView,不管是调用setMinHeight还是setMinimumHeight都可以设置最小高度
					textTv.setMinHeight(300);
//					textTv.setMinimumHeight(300);
					textTv.setBackgroundColor(Color.RED);
					bodyLayout.addView(textTv);
				}
				//在没有设置setLayoutParams的高为LayoutParams.MATCH_PARENT的情况下,通过setMaxHeight是可以设置最大高度的
				TextView textTv2 = new TextView(this);
				if (textTv2!=null) {
//					params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//					if (params!=null) {
//						textTv2.setLayoutParams(params);
//					}
					textTv2.setText("测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd" +
							"测试2dddddddddddddddddddd");
					textTv2.setMaxHeight(100);
					textTv2.setBackgroundColor(Color.WHITE);
					bodyLayout.addView(textTv2);
				}
				//Button
				Button btn = new Button(this);
				if (btn!=null) {
					btn.setText("Button");
					//Button无法通过setMinHeight设置最小高度
					textTv.setMinHeight(100);
					//Button通过setMinimumHeight可以设置最小高度
//					btn.setMinimumHeight(100);
					btn.setBackgroundColor(Color.BLUE);
					bodyLayout.addView(btn);
				}
				ImageView imageView = new ImageView(this);
				if (imageView!=null)
				{
					imageView.setImageResource(R.drawable.ic_launcher);
					//ImageView因为继承的是View所以没有setMinHeight方法,
//					imageView.setMinHeight(300);
					//ImageView通过setMinimumHeight可以设置最小高度
					imageView.setMinimumHeight(300);
					bodyLayout.addView(imageView);
				}
				EditText editText = new EditText(this);
				if (editText!=null)
				{
					//EditText可以通过setMinHeight方法设置最小高度,效果和setMinimumHeight一样
					editText.setMinHeight(300);
					//EditText通过setMinimumHeight可以设置最小高度
//					editText.setMinimumHeight(300);
					editText.setHint("TEST");
					bodyLayout.addView(editText);
				}
			}
		} catch (Exception e) {
		}

	}
}

#3.总结
(1)LinearLayout等继承ViewGroup的布局,因为ViewGroup也是继承View的,所以LinearLayout也只有setMinimumHeight和setMinimumWidth设置最小宽高的方法,但是并没有设置最大宽高的方法。
(2)Button,EditView等都是继承了TextView,EditView可以通过setMinHeight设置最小高度,但是Button不行,Button必须通过setMinimumHeight才能设置最小高度。
#4.源码地址
http://download.csdn.net/download/lgywsdy/10156936

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值