20161107

Flagmentにclass Rectangle extends View を入れる

 

//BlankFragment.java

package com.example.kunihiro.myapplication;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.LinearLayout;

public class BlankFragment extends Fragment {

    public BlankFragment() {

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

                             Bundle savedInstanceState) {

        View view= inflater.inflate(R.layout.fragment_blank, container, false);

        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearlayout_in_fragment_blank_xml);

        linearLayout.addView(new Rectangle(getActivity()));

        return view;

    }

}

 

//Rectangle.java

package com.example.kunihiro.myapplication;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.view.View;

class Rectangle extends View {

    Paint paint = new Paint();

    public Rectangle(Context context) {

        super(context);

    }

    public void onDraw(Canvas canvas) {

        paint.setColor(Color.RED);

        Rect rect = new Rect(20, 56, 200, 112);

        canvas.drawRect(rect, paint );

    }

}

 

flagment_blank.xml

<LinearLayout 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"

    tools:context="com.example.kunihiro.myapplication.BlankFragment"

    android:id="@+id/linearlayout"

    android:weightSum="1"

    android:orientation="vertical">

    <LinearLayout

        android:orientation="vertical"

        android:layout_width="match_parent"

        android:layout_height="184dp"

        android:layout_weight="0.12"

        android:id="@+id/linearlayout_in_fragment_blank_xml"></LinearLayout>

    <Button

        android:text="Button"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/button2"

        android:layout_weight="0.12" />

</LinearLayout>

 

20161107

getLayoutInflater().inflate

ボタンでlayout1からlayout2へ

//MainActivity.java

package com.example.kunihiro.myapplication;

 

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.LinearLayout;

 

public class MainActivity extends AppCompatActivity {

         public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);

            final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.blanklayout_in_activity_main);

            findViewById(R.id.button_to_layout1).setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    linearLayout.removeAllViews();

                    getLayoutInflater().inflate(R.layout.layout1, linearLayout);

                }

            });

            findViewById(R.id.button_to_layout2).setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {

                    linearLayout.removeAllViews();

                    getLayoutInflater().inflate(R.layout.layout2, linearLayout);

                }

            });

        }

    }

 

//activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <LinearLayout

        android:layout_height="wrap_content"

        android:id="@+id/blanklayout_in_activity_main"

        android:layout_width="fill_parent"

        android:layout_weight="1"

        android:orientation="horizontal">

    </LinearLayout>

    <Button android:text="To layout1"

        android:id="@+id/button_to_layout1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

    </Button>

    <Button android:text="To layout2"

        android:id="@+id/button_to_layout2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

    </Button>

 

//layout1.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TextView

        android:text="This is layout1"

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="30sp" />

</LinearLayout>

 

//layout2.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent" >

    <TextView

        android:text="This is layout2"

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="30sp" />

</LinearLayout>