布局管理器(一般被称为是布局)是对ViewGroup类的扩展,是用来控制子控件在UI中的位置。java
Android SDK包含了许多布局类,在为视图、Fragment和Activity建立UI时,能够使用和修改这些类,还能够建立本身的布局类。ide
其实说白了,布局管理器或布局就是Layout的一种。布局
Android SDK提供一些经常使用的布局类:FrameLayout、LinearLayout、RelativeLayout和GridLayout。this
通常实现布局,是使用XML文件的形式定义的。在XML中实现布局能够把表示层从视图、Fragment和Activity代码中分离出来。也能够建立支持特定硬件的、无需修改代码就能够动态加载的变体。blog
若是状况须要,也能够使用代码实现布局。好比下述状况实现:it
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LogUtil.d(TAG, "onCreate.."); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); TextView textView = new TextView(this); textView.setText("Enter Text Below"); EditText editText = new EditText(this); editText.setText("Text Goes Here!"); int lWidth = LinearLayout.LayoutParams.MATCH_PARENT; int lHeight = LinearLayout.LayoutParams.WRAP_CONTENT; ll.addView(textView, new LinearLayout.LayoutParams(lWidth, lHeight)); ll.addView(editText, new LinearLayout.LayoutParams(lWidth, lHeight)); setContentView(ll); }
实现的布局以下:io