THIẾT KẾ LAYOUT ANDROID BẰNG CODE JAVA

THIẾT KẾ LAYOUT ANDROID BẰNG CODE JAVA

2020-10-12 1711 lượt xem
Trương Thanh Hùng
Mục lục [ hide ]
public void createLinnearLayoutView() {
    linearLayoutRoot = new LinearLayout(this);
    linearLayoutRoot.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    linearLayoutRoot.setOrientation(LinearLayout.VERTICAL);

    LinearLayout linearLayoutSub = new LinearLayout(this);
    linearLayoutSub.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    linearLayoutSub.setOrientation(LinearLayout.HORIZONTAL);

    ImageView imageView = new ImageView(this);
    imageView.setImageResource(R.mipmap.ic_launcher);

    TextView textView = new TextView(this);
    textView.setText("Xin Chào! Tôi là ngôn ngữ lập trình Android");
    LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lay.gravity = Gravity.CENTER;
    textView.setLayoutParams(lay);

    linearLayoutSub.addView(imageView);
    linearLayoutSub.addView(textView);

    Button btnClick = new Button(this);
    btnClick.setText("Click để xem");
    btnClick.setLayoutParams(lay);

    Button btnHihi = new Button(this);
    btnHihi.setText("Hihi Đồ Ngốc");
    btnHihi.setLayoutParams(lay);

    linearLayoutRoot.addView(linearLayoutSub);
    linearLayoutRoot.addView(btnClick);
    linearLayoutRoot.addView(btnHihi);

}
bài viết trong chủ đề