安卓的UI页面都是有View 和ViewGroup及其派生子类组合而成的;
View是所有UI组件的基类,而viewgroup是容纳这些组件的容器,其本身也是从view类派生出来的
view直接派生出的子类有
Analogclock,image view,keyboardview,progress八日,surfaceview,textview,viewgroup,viewstub
viewgroup派生出直接子类有:
absoluteLayout,AdapterView,FragmentBreadCrumbs,FrameLayout,LinearLayout,RelativeLayout,SlidingDrawe
具体代码如下:
import android.graphics.Color;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.*;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = View.inflate(this, R.layout.activity_main, null);//引用view模块
TextView textView = new TextView(this);//引入文本框TextView
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);//通过layoutParams来调整框体大小。
textView.setLayoutParams(layoutParams);//将textView加入到LayoutParams框架中
textView.setBackgroundColor(Color.GREEN);//设置textView的背景显示颜色
textView.setText("hhahah");//设置textView的文本
((ConstraintLayout) view).addView(textView);将view进行强制转换成mainActivity中原本的constraintLayout类型,然后将textView添加到view模块中
setContentView(view);//最后显示view
}
}