반응형
확인버튼을 누르면 체크박스를 확인
체크되어 있지 않으면, product layout을
체크되어 있으면 advertise layout을 inflate 하는 예 입니다.
activity_main.xml 이외에
layout_ad.xml, layout_product.xml이 필요합니다.
----------------------------------------------------
MainActivity.java
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 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;@Overrideprotected 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());}@Overridepublic 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;}@Overridepublic 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 SimplifiableIfStatementif (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}protected class btnSelectClickListener implements View.OnClickListener{@Overridepublic 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344 <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"><LinearLayoutandroid: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"><CheckBoxandroid: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" /><Buttonandroid: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><LinearLayoutandroid: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
1234567891011121314151617181920212223 <?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"><TextViewandroid: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" /><ImageViewandroid: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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 <?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"><TextViewandroid: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" /><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@+id/textView2"android:layout_centerHorizontal="true"><RadioGroupandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:paddingLeft="20dp"><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="LG G4"android:id="@+id/radioButton"android:textSize="30dp"android:checked="false" /><RadioButtonandroid: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
반응형
'Android' 카테고리의 다른 글
[Android GCM] 1. 준비 (0) | 2015.07.10 |
---|---|
[Android] Intent 예제 (0) | 2015.05.19 |
안드로이드 푸시 기능 구현 (4) - 결과 (0) | 2015.01.16 |
안드로이드 푸시 기능 구현 (3) - Nodejs 푸시 서버 (0) | 2015.01.14 |
안드로이드 푸시 기능 구현 (2) - 안드로이드 앱 (0) | 2015.01.12 |
댓글