LoginSignup
3
4

More than 5 years have passed since last update.

カスタム属性の設定、取得方法

Last updated at Posted at 2012-04-17

①属性を宣言

attrs.xml
<declare-styleable name="ActionButton">
    <attr name="ident">
        <enum name="LAB" value="-300" />
        <enum name="RAB" value="-400" />
        <enum name="LAB2" value="-500" />
        <enum name="RAB2" value="-600" />
        <enum name="MAB" value="-700" />
    </attr>
</declare-styleable>

 
②ビューに属性を記述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:launcher="http://schemas.android.com/apk/res/XXXXX" <!--アプリケーションのパッケージを設定-->

    <ActionButton>
        android:src="@drawable/ab_empty"
        android:background="@drawable/lab2_bg"
        android:scaleType="center"
        android:layout_height="wrap_content"
        android:id="@+id/btn_lab2"
        launcher:ident="LAB2" <!--属性を指定-->
        android:layout_width="64dip"
        launcher:direction="horizontal"
    </ActionButton>
</LinearLayout>

 
 
③属性を取得

ActionButton.java
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.ActionButton,defStyle,0);
int mIdent=a.getInt(R.styleable.ActionButton_ident, mIdent); // ここで取得!
a.recycle();
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4