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

来源:互联网 发布:怎么查网络是否丢包 编辑:程序博客网 时间:2024/06/06 11:26

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和setMinimumHeightandroid: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

原创粉丝点击