AndroidのCustomView

```xml:activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:original="http://schemas.android.com/apk/res-auto"
    tools:context="com.******.myapplication.myapplication.MainActivity">



    <com.*****.myapplication.CustomLayout
        android:id="@+id/custom"
        original:customText="ごりらっぱ"
        original:customColors="#ff00"
        original:customSize="38dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />




</RelativeLayout>

```

```java:CustomLayout.java

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


public class CustomLayout extends LinearLayout {
    private Context context;
    public CustomLayout(Context context, AttributeSet attr) {
        super(context, attr);
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
        TypedArray t_array =
                context.obtainStyledAttributes(
                        attr,
                        R.styleable.CustomLayout
                );

        int default_dimen_size = 18;
        int default_color = 0xFFFFFFFF;
        String str = t_array.getString(R.styleable.CustomLayout_customText);
        int color = t_array.getColor(R.styleable.CustomLayout_customColors, default_color);
        int dimen = t_array.getDimensionPixelSize(R.styleable.CustomLayout_customSize, default_dimen_size);

        EditText txt = (EditText)view.findViewById(R.id.txt);
        txt.setText(str);
        txt.setTextColor(color);
        txt.setTextSize(dimen);
    }
    public void say() {
        Toast.makeText(context, "タップ", Toast.LENGTH_SHORT).show();
    }
}

```

```xml:res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomLayout">
        <attr name="customText" format="string" />
        <attr name="customColors" format="color" />
        <attr name="customSize" format="dimension" />
    </declare-styleable>
</resources>

```

```java:MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomLayout custom = (CustomLayout)findViewById(R.id.custom);
        custom.say();

    }
}

```

```xml:custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <EditText
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center"
            android:textSize="50px" />
</LinearLayout>

```