layout은 화면구성을 정의해놓은 파일로

화면에 보여지도록 하기위해 java code가 필요하다

*.xml에서 정의한 layout을 Inflater를 통해 객체화를하고

객체화를 한후 *.java를 통해 화면에 보이도록 한다.

 

setContentView(R.layout.*) method를통해

화면에 보여줄 view를 지정(매개변수) 하고

객체화(memory loading)을 한다

R은 app/res를 의미한다

 

setContentView 는 전체화면만 객체화를하고

LayInflater를 통해 부분작업 객체화를 한다

activity_main.xml

<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inflation test"
android:layout_marginTop = "30dp"
android:textSize = "30sp"
android:textColor = "#a63"
android:layout_gravity = "center"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text = "add layout"
android:textSize = "30sp"
android:onClick="onClickedBtn"/>
<LinearLayout
android:id="@+id/addLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>

add_layout.xml

<Button
android:id="@+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "test"
android:textSize = "20sp"
android:textStyle = "bold"/>
<RadioGroup
android:id="@+id/radioGroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<RadioButton
android:id="@+id/radioBtn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sport"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
<RadioButton
android:id="@+id/radioBtn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="travel"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
<RadioButton
android:id="@+id/radioBtn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reading"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
<RadioButton
android:id="@+id/radioBtn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="gaming"
android:textColor="#00f"
android:textStyle="bold"
android:textSize="20dp"
android:layout_marginRight="5dp"/>
</RadioGroup>
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_margin="10dp"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="accept"
android:textSize="20sp"
android:layout_margin="10dp"
android:textColor="#f00"/>
<CheckBox
android:id="@+id/agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//전체화면에 대한 뷰를 지정하고, inflation을 한다
}
//when button is clicked, add layout
public void onClickedBtn(View v){
addLayout();
}
//add_layout.sml을 activity_main.xml에 추가하는 기능의 method
private void addLayout(){
LinearLayout parentLayout = findViewById(R.id.addLayout);

//inflation
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.add_layout, parentLayout, true);

Button btnTest = findViewById(R.id.btnTest);
final CheckBox agree = findViewById(R.id.agree);

btnTest.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(agree.isChecked()){
agree.setChecked(false);
}else{
agree.setChecked(true);
}
}
});
}
}

'android studio' 카테고리의 다른 글

버튼 style변경(onDraw(), invalidate())  (0) 2019.08.12
toast message, style  (0) 2019.08.11
GridView  (0) 2019.08.11
Spinner  (0) 2019.08.10
AdapterView(ListView)  (0) 2019.08.10

+ Recent posts