Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
2
Добавлен:
14.12.2020
Размер:
183.59 Кб
Скачать

Код:MainActivity.java

package com.example.myapplicationz; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // создание LinearLayout LinearLayout linLayout = new LinearLayout(this); // установим вертикальную ориентацию linLayout.setOrientation(LinearLayout.VERTICAL); // создаем LayoutParams LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); // устанавливаем linLayout как корневой элемент экрана setContentView(linLayout, linLayoutParam); LinearLayout.LayoutParams lpView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); TextView tv = new TextView(this); tv.setText("TextView"); tv.setLayoutParams(lpView); linLayout.addView(tv); Button btn = new Button(this); btn.setText("Button"); linLayout.addView(btn, lpView); LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); leftMarginParams.leftMargin = 50; Button btn1 = new Button(this); btn1.setText("Button1"); linLayout.addView(btn1, leftMarginParams); LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); rightGravityParams.gravity = Gravity.RIGHT; Button btn2 = new Button(this); btn2.setText("Button2"); linLayout.addView(btn2, rightGravityParams); } }

Код:activity main

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

Соседние файлы в папке Android studio projects