관리 메뉴

나만을 위한 블로그

[Android] LayoutInflater를 사용한 텍스트뷰 동적 추가하기 본문

Android

[Android] LayoutInflater를 사용한 텍스트뷰 동적 추가하기

참깨빵위에참깨빵 2019. 12. 10. 13:09
728x90
반응형

참고한 사이트 : https://coding-factory.tistory.com/44

 

 

앱 개발 공부를 하다가 동적으로 텍스트뷰를 만들어야 할 일이 생겼다.

기왕이면 background를 블랙으로 설정하고 글자 색깔도 흰색으로 해야 했다.

그러다 찾은 것이 리사이클러뷰에서 자주 사용했던 LayoutInflater다.

 

 

LayoutInflater : xml에 정의된 리소스들을 뷰 형태로 반환해주는 역할을 하는 것, 보통 자바 코드에서 뷰, 뷰그룹을 사용하거나 다이얼로그, 어댑터 등에서 배경화면이 될 레이아웃을 만들어놓고 뷰 형태로 반환받아 액티비티를 실행하게 된다.

 

 

설명을 보니 onCreate()의 setContentView()와 비슷하다.

나는 액티비티에서 버튼을 눌러 텍스트뷰를 보여줘야 했는데 이 부분에 대해서 잘 생각해보면 해결할 수 있겠다.

아래는 위 사이트의 예제 코드다.

 

MainActivity.java

 

public class MainActivity extends AppCompatActivity {

Button add_view;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

add_view = (Button) findViewById(R.id.add_layout);

add_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

SubActivity n_layout = new SubActivity(getApplicationContext());

LinearLayout con = (LinearLayout) findViewById(R.id.con);

con.addView(n_layout);

Button but = (Button) con.findViewById(R.id.b1);

but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "클릭되었습니다.", Toast.LENGTH_LONG).show();
}
});
}
});
}
}

 

 

MainActivity xml 파일

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_layout"
android:text="레이아웃 추가"/>

<LinearLayout
android:id="@+id/con"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/add_layout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</LinearLayout>

</RelativeLayout>

 

 

SubActivity.java

 

 

public class SubActivity extends LinearLayout { // 동적으로 만들것이라 그런가 리니어 레이아웃을 상속한다

// public SubActivity(Context context, AttributeSet attrs) { // 이 생성자는 없어도 된다
// super(context, attrs);
//
// init(context);
// }

public SubActivity(Context context) {
super(context);

init(context);
}
private void init(Context context){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.activity_sub, this, true);
}
}

 

 

SubActivity xml 파일

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="내부 레이아웃"
android:textSize="30dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="내부 레이아웃 버튼"/>

</LinearLayout>

</LinearLayout>

 

 

이 코드를 치고 빌드하면 좌측 상단에 버튼이 하나 있다.

버튼을 누르면 내부 레이아웃이라 적힌 텍뷰와 내부 레이아웃 버튼이 생겨나고, 같은 버튼을 계속 누르면 그 밑으로 계속해서 텍뷰와 버튼이 생겨난다.

반응형
Comments