안드로이드

 

 

 

 

  

 

 

class MainActivity

package org.androidtown.mylayoutinflater;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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


    public void onButton1Clicked(View v){
        SubLayout layout1=new SubLayout(this);

        LinearLayout container =(LinearLayout)findViewById(R.id.container);
        container.addView(layout1);
    }



}

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.androidtown.mylayoutinflater.MainActivity">

    <Button
        android:text="메인버튼"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="onButton1Clicked"
        />

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


</RelativeLayout>

 

 

class SubLayout

package org.androidtown.mylayoutinflater;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

public class SubLayout extends LinearLayout {

    public SubLayout(Context context) {
        super(context);
        init(context);
    }

    public SubLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context){

        LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //true 이면 바로 실행, false 이면 필요한 시기에
        inflater.inflate(R.layout.sub_layout, this, true);

    }



}

 

sub_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_sub_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="org.androidtown.mylayoutinflater.SubLayout">

    <Button
        android:text="서브레이아웃 버튼"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="194dp"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="97dp"
        android:layout_marginStart="97dp"
        android:background="@android:color/holo_blue_dark" />
</RelativeLayout>

 

 

 

 

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

건강을 유지하기 위해서는 하루에 적어도 40가지에 이르는 영양소를 섭취해야만 한다. 즉 "슈퍼식품"이라고 알려진 마늘, 밀기울 등만 섭취하는 것보다는 평범한 식품 여러 가지를 섭취하는 것이 낫다. - New Life 편집부

댓글 ( 4)

댓글 남기기

작성