728x90
Main_Activity.class
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup layout = findViewById(R.id.container);
String message = "Java 소스를 통한 화면 구성";
TextView tv = new TextView(this);
tv.setTextSize(20);
tv.setText(message);
Button btn = new Button(this);
btn.setText("토스트 띄우기");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "뷰 화면 띄우기", Toast.LENGTH_SHORT).show();
}
});
DisplayMetrics metrics = getResources().getDisplayMetrics();
int widthPixels = metrics.widthPixels * 1;
int heightPixels = widthPixels * 1;
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setBackgroundResource(R.color.teal_200);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(widthPixels,heightPixels);
linearLayout.setLayoutParams(params);
Button tBtn = new Button(this);
tBtn.setText("Java 소스로 만들어진 레이아웃의 차일드");
// tBtn.setBackgroundColor(Color.WHITE);
layout.addView(tv);
layout.addView(btn);
layout.addView(linearLayout);
linearLayout.addView(tBtn);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/container"
>
</LinearLayout>
728x90
반응형
'안드로이드 기반 앱 개발부 > 자바 코드 개발청' 카테고리의 다른 글
안드로이드 번들(Bundle) (0) | 2021.03.31 |
---|---|
레이아웃 인플레이션 (0) | 2021.03.29 |