Android-custom-components

提供:Dev Guides
移動先:案内検索

Android-カスタムコンポーネント

'_独自の定義済みクラスでサブクラスを拡張することで、事前に組み込まれたコンポーネントに独自のコンポーネントを実装する_

Androidは、Button、TextView、EditText、ListView、CheckBox、RadioButton、Gallery、Spinner、AutoCompleteTextViewなどの事前作成済みウィジェットの素晴らしいリストを提供します。 Androidアプリケーション開発で直接使用できますが、使用可能なウィジェットの既存の機能に満足できない場合があります。 Androidは、ニーズに合わせてカスタマイズできる独自のカスタムコンポーネントを作成する手段を提供します。

既存のウィジェットまたはレイアウトを少し調整するだけでよい場合は、ウィジェットまたはレイアウトをサブクラス化し、そのメソッドをオーバーライドするだけで、画面要素の外観と機能を正確に制御できます。

このチュートリアルでは、簡単で簡単な手順を使用して、カスタムビューを作成し、アプリケーションで使用する方法を説明します。

カスタム

カスタムビュー階層のカスタムコンポーネントの例

シンプルなカスタムコンポーネントの作成

Step Description
1 You will use Android studio IDE to create an Android application and name it as myapplication under a package com.example.finddevguides7.myapplication as explained in the Hello World Example chapter.
2 Create an XML res/values/attrs.xml file to define new attributes along with their data type.
3 Create src/mainactivity.java file and add the code to define your custom component
4 Modify res/layout/activity_main.xml file and add the code to create Colour compound view instance along with few default attributes and new attributes.
5 Run the application to launch Android emulator and verify the result of the changes done in the application.

res/valuesフォルダーにattrs.xmlという次の属性ファイルを作成します。

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="TimeView">
      <declare-styleable name="TimeView">
         <attr name="title" format="string"/>
         <attr name="setColor" format="boolean"/>
      </declare-styleable>
   </declare-styleable>
</resources>

アクティビティで使用されるレイアウトファイルを次のように変更します。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity" >

   <com.example.finddevguides7.myapplication.TimeView
      android:id="@+id/timeView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textColor="#fff"
      android:textSize="40sp"
      custom:title="my time view"
      custom:setColor="true"/>

   <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/simple"
      android:layout_below="@id/timeView"
      android:layout_marginTop="10dp"/>
</RelativeLayout>

複合ビュー用に、timeviewという次のJavaファイルを作成します。

package com.example.finddevguides7.myapplication;
/**
 *Created by finddevguides7 on 9/14/2016.
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.res.TypedArray;

import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;

public class TimeView extends TextView {
   private String titleText;
   private boolean color;

   public TimeView(Context context) {
      super(context);
      setTimeView();
   }

   public TimeView(Context context, AttributeSet attrs) {
      super(context, attrs);
     //retrieved values correspond to the positions of the attributes
         TypedArray typedArray = context.obtainStyledAttributes(attrs,
            R.styleable.TimeView);
      int count = typedArray.getIndexCount();
      try{

         for (int i = 0; i < count; ++i) {

            int attr = typedArray.getIndex(i);
           //the attr corresponds to the title attribute
            if(attr == R.styleable.TimeView_title) {

              //set the text from the layout
               titleText = typedArray.getString(attr);
               setTimeView();
            } else if(attr == R.styleable.TimeView_setColor) {
              //set the color of the attr "setColor"
               color = typedArray.getBoolean(attr, false);
               decorateText();
            }
         }
      }

     //the recycle() will be executed obligatorily
      finally {
        //for reuse
         typedArray.recycle();
      }
   }

   public TimeView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      setTimeView();
   }

   private void setTimeView() {
     //has the format hour.minuits am/pm
      SimpleDateFormat dateFormat = new SimpleDateFormat("hh.mm aa");
      String time = dateFormat.format(Calendar.getInstance().getTime());

      if(this.titleText != null )
      setText(this.titleText+" "+time);
      else
         setText(time);
   }

   private void decorateText() {
     //when we set setColor attribute to true in the XML layout
      if(this.color == true){
        //set the characteristics and the color of the shadow
         setShadowLayer(4, 2, 2, Color.rgb(250, 00, 250));
         setBackgroundColor(Color.CYAN);
      } else {
         setBackgroundColor(Color.RED);
      }
   }
}

メインアクティビティjavaファイルを次のコードに変更し、アプリケーションを実行します。

package com.example.finddevguides7.myapplication;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

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

      TextView simpleText = (TextView) findViewById(R.id.simple);
      simpleText.setText("That is a simple TextView");
   }
}

実行中のアプリケーションは、次のスクリーンショットのようになります。

カスタム