Skip to content

Instantly share code, notes, and snippets.

@woshidan
Created December 19, 2015 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woshidan/2fa05bb429fb8a045853 to your computer and use it in GitHub Desktop.
Save woshidan/2fa05bb429fb8a045853 to your computer and use it in GitHub Desktop.
CustomViewを作る
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
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">
<!-- 属性値がデフォルトのスタイルから設定されているのを見る例 -->
<com.example.woshidan.customviewtest.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- 属性値をxmlからセットする例 -->
<com.example.woshidan.customviewtest.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:name="MasterUser"
custom:email="master@example.com"
custom:grade="master"
/>
</LinearLayout>
package com.example.woshidan.customviewtest;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by woshidan on 2015/12/19.
*/
public class CustomView extends RelativeLayout {
private String mName;
private String mEmail;
private int mGrade;
private TextView mNameView;
private TextView mEmailView;
private TextView mGradeView;
private static final int BEGINNER = 0;
private static final int MASTER = 1;
private View layout;
public CustomView(Context context) {
this(context, null);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.d("CustomView", "Constructor");
// 参考: http://yaraki.github.io/slides/gdm01/#19
// 属性リストを取得
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CustomView, // 属性の定義
defStyleAttr,
R.style.Widget_CustomViewTest_CustomView); // デフォルトの属性値
// このViewGroupの親要素はマージの形で元の要素の箇所に渡されます。
inflate(context, R.layout.view_custom, this);
// カスタムViewの中の各Viewの要素を取得
mNameView = (TextView) this.findViewById(R.id.name);
mEmailView = (TextView) this.findViewById(R.id.email);
mGradeView = (TextView) this.findViewById(R.id.grade);
// 属性値を取得してViewにセット
setEmail(a.getString(R.styleable.CustomView_name));
setName(a.getString(R.styleable.CustomView_email));
setGrade(a.getInt(R.styleable.CustomView_grade, 0));
// 忘れずに
a.recycle();
}
public void setName(String name) {
mName = name;
mNameView.setText(mName);
}
public void setEmail(String email) {
mEmail = email;
mEmailView.setText(mEmail);
}
public void setGrade(int grade) {
mGrade = grade;
if (mGrade == BEGINNER) {
mGradeView.setText(getResources().getText(R.string.grade_begginer));
mGradeView.setBackgroundColor(getResources().getColor(R.color.grade_beginner));
// API23以上でないとdeprectedでない方のgetColor()は動作しません(本当は処理をAPIバージョンで分けるのが推奨)
} else {
mGradeView.setText(getResources().getText(R.string.grade_master));
mGradeView.setBackgroundColor(getResources().getColor(R.color.grade_master));
}
}
}
<!-- そんなに量ないので、まとめて -->
<!-- attrs.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- http://yaraki.github.io/slides/gdm01/#16 -->
<!-- formatの各属性については http://qiita.com/Hoshi_7/items/57c3a79c43efe05b5368 -->
<declare-styleable name="CustomView">
<attr name="name" format="string" />
<attr name="email" format="string" />
<!-- enum値 -->
<attr name="grade">
<enum name="beginner" value="0"/>
<enum name="master" value="1"/>
</attr>
</declare-styleable>
</resources>
<!-- styles.xml -->
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<!-- CustomView用 -->
<style name="Widget.CustomViewTest.CustomView" parent="android:Widget" >
<item name="name">testUser</item>
<item name="email">test@example.com</item>
<item name="grade">beginner</item>
</style>
</resources>
<!-- colors.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="grade_master">#082</color>
<color name="grade_beginner">#ec0</color>
</resources>
<!-- strings.xml -->
<resources>
<string name="app_name">CustomViewTest</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="grade_begginer">begginer</string>
<string name="grade_master">master</string>
</resources>
<!-- string.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#66f"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:id="@+id/name"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="20dp"
android:text="UserName"/>
<TextView
android:id="@+id/email"
android:layout_alignParentBottom="true"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="15dp"
android:text="test@example.com"/>
<TextView
android:id="@+id/grade"
android:layout_marginBottom="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/name"
android:padding="4dp"
android:background="@color/grade_beginner"
android:textStyle="bold"
android:textColor="#fff"
android:text="@string/grade_begginer"/>
</RelativeLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment