본문 바로가기
Android

[Android] layout Inflater 예제

by GGoris 2015. 5. 19.
반응형

확인버튼을 누르면 체크박스를 확인

체크되어 있지 않으면, product layout을

체크되어 있으면 advertise layout을 inflate 하는 예 입니다.


activity_main.xml 이외에

layout_ad.xml, layout_product.xml이 필요합니다.


   





----------------------------------------------------

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.ex1_150519;
 
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
 
 
public class MainActivity extends ActionBarActivity {
 
    Button btnSelect;
    CheckBox cbAds;
    LinearLayout contentsViewer;
    LayoutInflater inflater;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btnSelect = (Button) findViewById(R.id.btnSelect);
        cbAds = (CheckBox) findViewById(R.id.cbAds);
        contentsViewer = (LinearLayout) findViewById(R.id.contentsViewer);
        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
        btnSelect.setOnClickListener(new btnSelectClickListener());
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
 
 
 
    protected class btnSelectClickListener implements View.OnClickListener{
 
        @Override
        public void onClick(View v) {
            if(cbAds.isChecked()){
                contentsViewer.removeAllViews();
                inflater.inflate(R.layout.layout_ad, contentsViewer, true);
            } else {
                contentsViewer.removeAllViews();
                inflater.inflate(R.layout.layout_product, contentsViewer, true);
            }
        }
    }
}
 
 
cs

----------------------------------------------------

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:id="@+id/linearLayout">
 
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADs"
            android:id="@+id/cbAds"
            android:textSize="30dp"
            android:checked="false"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp" />
 
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="확인"
            android:id="@+id/btnSelect"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:textSize="30dp" />
    </LinearLayout>
 
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/linearLayout"
        android:id="@+id/contentsViewer"></LinearLayout>
</RelativeLayout>
cs


----------------------------------------------------

layout_ad.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Advertisements"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:textSize="40dp" />
 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/imageView"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>
 
cs



----------------------------------------------------

layout_product.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Products"
        android:id="@+id/textView2"
        android:layout_marginTop="50dp"
        android:textSize="40dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
 
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true">
 
        <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingLeft="20dp">
 
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LG G4"
                android:id="@+id/radioButton"
                android:textSize="30dp"
                android:checked="false" />
 
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Samsung Gallaxy S6"
                android:id="@+id/radioButton2"
                android:textSize="30dp"
                android:checked="false" />
        </RadioGroup>
    </LinearLayout>
</RelativeLayout>
 
cs




반응형

댓글