Viewクラス

android.view.Viewは、すべてのウィジェットのスーパークラスです。

Android 3.0以降では、Viewの操作はUIスレッドから行わなければなりません。さもなくばCalledFromWrongThreadException例外が発生します。

final TextView textView = (TextView)findViewById(R.id.test);

Thread thread = new Thread() {
    public void run() {
        textView.setText("A"); // ERROR : CalledFromWrongThreadException
    }
};
thread.start();

textView.setText("B"); // OK

クラス階層

属性 (Attributes)

属性 説明
android:alpha alpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque).
android:background A drawable to use as the background.
android:clickable Defines whether this view reacts to click events.
android:contentDescription ビューの内容を簡単に説明する文字列
android:drawingCacheQuality Defines the quality of translucent drawing caches.
android:duplicateParentState When this attribute is set to true, the view gets its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.
android:fadingEdgeLength Defines the length of the fading edges.
android:filterTouchesWhenObscured Specifies whether to filter touches when the view's window is obscured by another visible window.
android:fitsSystemWindows Boolean internal attribute to adjust view layout based on system windows such as the status bar.
android:focusable Boolean that controls whether a view can take focus.
android:focusableInTouchMode Boolean that controls whether a view can take focus while in touch mode.
android:hapticFeedbackEnabled Boolean that controls whether a view should have haptic feedback enabled for events such as long presses.
android:id ビューに識別子名を設定する。これは後でView.findViewById()またはActivity.findViewById()でビューを検索するために使用される
android:isScrollContainer Set this if the view will serve as a scrolling container, meaing that it can be resized to shrink its overall window so that there will be space for an input method.
android:keepScreenOn Controls whether the view's window should keep the screen on while visible.
android:layerType Specifies the type of layer backing this view.
android:longClickable Defines whether this view reacts to long click events.
android:minHeight Defines the minimum height of the view.
android:minWidth Defines the minimum width of the view.
android:nextFocusDown Defines the next view to give focus to when the next focus is FOCUS_DOWN If the reference refers to a view that does not exist or is part of a hierarchy that is invisible, a RuntimeException will result when the reference is accessed.
android:nextFocusForward Defines the next view to give focus to when the next focus is FOCUS_FORWARD If the reference refers to a view that does not exist or is part of a hierarchy that is invisible, a RuntimeException will result when the reference is accessed.
android:nextFocusLeft Defines the next view to give focus to when the next focus is FOCUS_LEFT.
android:nextFocusRight Defines the next view to give focus to when the next focus is FOCUS_RIGHT If the reference refers to a view that does not exist or is part of a hierarchy that is invisible, a RuntimeException will result when the reference is accessed.
android:nextFocusUp Defines the next view to give focus to when the next focus is FOCUS_UP If the reference refers to a view that does not exist or is part of a hierarchy that is invisible, a RuntimeException will result when the reference is accessed.
android:onClick Name of the method in this View's context to invoke when the view is clicked.
android:padding Sets the padding, in pixels, of all four edges.
android:paddingBottom Sets the padding, in pixels, of the bottom edge; see padding.
android:paddingLeft Sets the padding, in pixels, of the left edge; see padding.
android:paddingRight Sets the padding, in pixels, of the right edge; see padding.
android:paddingTop Sets the padding, in pixels, of the top edge; see padding.
android:requiresFadingEdge Defines which edges should be faded on scrolling.
android:rotation rotation of the view, in degrees.
android:rotationX rotation of the view around the x axis, in degrees.
android:rotationY rotation of the view around the y axis, in degrees.
android:saveEnabled If unset, no state will be saved for this view when it is being frozen.
android:scaleX scale of the view in the x direction.
android:scaleY scale of the view in the y direction.
android:scrollX The initial horizontal scroll offset, in pixels.
android:scrollY The initial vertical scroll offset, in pixels.
android:scrollbarAlwaysDrawHorizontalTrack Defines whether the horizontal scrollbar track should always be drawn.
android:scrollbarAlwaysDrawVerticalTrack Defines whether the vertical scrollbar track should always be drawn.
android:scrollbarDefaultDelayBeforeFade Defines the delay in milliseconds that a scrollbar waits before fade out.
android:scrollbarFadeDuration Defines the delay in milliseconds that a scrollbar takes to fade out.
android:scrollbarSize Sets the width of vertical scrollbars and height of horizontal scrollbars.
android:scrollbarStyle Controls the scrollbar style and position.
android:scrollbarThumbHorizontal Defines the horizontal scrollbar thumb drawable.
android:scrollbarThumbVertical Defines the vertical scrollbar thumb drawable.
android:scrollbarTrackHorizontal Defines the horizontal scrollbar track drawable.
android:scrollbarTrackVertical Defines the vertical scrollbar track drawable.
android:scrollbars Defines which scrollbars should be displayed on scrolling or not.
android:soundEffectsEnabled Boolean that controls whether a view should have sound effects enabled for events such as clicking and touching.
android:tag Supply a tag for this view containing a String, to be retrieved later with View.getTag() or searched for with View.findViewWithTag().
android:transformPivotX x location of the pivot point around which the view will rotate and scale.
android:transformPivotY y location of the pivot point around which the view will rotate and scale.
android:translationX translation in x of the view.
android:translationY translation in y of the view.
android:visibility Controls the initial visibility of the view.

ID

IDはリソースを識別するためのもので、通常レイアウトファイルandroid:id="@+id/"my_idで割り当てます。

IDを割り当てたViewは、findViewById()を使用して

findViewById( R.id.my_id )

で取得できます。

タグ (Tags)

IDとは異なり、タグはビューを特定するためには使用されません。ビューと関連する情報を保持することに用いられます。

位置 (Position)

getLeft()で左端、getTop()で上端の座標をピクセル単位で取得できます。またgetRight()で右端の座標を取得できますが、これはgetLeft() + getWidth()の値に等しいです。

サイズ (Size)

サイズはViewの幅と高さを表し、その計算方法によりそれぞれ2種類あります。

分類 サイズ メソッド 説明
Measured measured width getMeasuredWidth() 親となるViewの内側に収まるためにどれくらいのサイズが必要とされているかを表します。これらはレイアウトの処理において、mesure()の実行後でなければ取得できません。
measured height getMeasuredHeight()
Drawing width getWidth() スクリーンに描画された実際のサイズを表します。
height getHeight()

パディング (Padding)

パディングの設定
XML属性 メソッド 説明
android:padding setPadding(int, int, int, int)  
android:paddingBottom なし  
android:paddingLeft  
android:paddingRight  
android:paddingTop  
パディングの取得
メソッド 説明
getPaddingLeft()  
getPaddingTop()  
getPaddingRight()  
getPaddingBottom()  

マージン (Margin)

Viewはマージンをサポートしていないため、マージンを適用するにはViewGroupのMarginLayoutParamsを使用します。

レイアウト (Layout)

レイアウトは2つの手順によって処理されます。Measure passはmesure()で処理され、Layout passはlayout()で呼び出されます。

メソッド (Method)

ビュー (View)

findViewById

public final View findViewById( int id )
findViewById - View | Android Developers

指定IDのビューを取得できます。

フォーカス (Focus)

メソッド 説明
requestFocus() 特定のビューにフォーカスを合わせる
setFoocusable() フォーカスできるかどうかを設定する
isFocusable() フォーカスできるかどうかを取得する

フォーカスはキーパッド向けの機能であり、タッチモードでは意味を持ちません。

スクロール (Scrolling)

メソッド 説明
scrollBy() スクロールする
scrollTo() スクロールする
awakenScrollBars() スクロールバーを見えるようにする
isHorizontalScrollBarEnabled() 水平スクロールバーを描画するかどうかを取得する
isVerticalScrollBarEnabled() 垂直スクロールバーを描画するかどうかを取得する
setHorizontalScrollBarEnabled() 水平スクロールバーを描画するかどうかを設定する
setVerticalScrollBarEnabled() 垂直スクロールバーを描画するかどうかを設定する

セキュリティ (Security)

他のビューによって表示が隠されているときに、タッチを無効にできます。設定はsetFilterTouchesWhenObscured()で行います。

public void setFilterTouchesWhenObscured( boolean enabled )
setFilterTouchesWhenObscured - View | Android Developers

可視性 (visibility)

setVisibility()で、Viewの表示や非表示ができます。

XML属性 メソッド
android:visibility
public void setVisibility( int visibility )
setVisibility - View | Android Developers
 
public int getVisibility()
パラメータ
メソッドのパラメータ XML属性値 設定内容
表示 領域の確保
View.VISIBLE visible 0
View.INVISIBLE invisible 1 ×
View.GONE gone 2 × ×

イベント (Event)

カテゴリ メソッド 説明
Creation Constructors There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file.
onFinishInflate() Called after a view and all of its children has been inflated from XML.
Layout onMeasure(int, int) Called to determine the size requirements for this view and all of its children.
onLayout(boolean, int, int, int, int) Called when this view should assign a size and position to all of its children.
onSizeChanged(int, int, int, int) Called when the size of this view has changed.
Drawing onDraw(Canvas) Called when the view should render its content.
Event processing onKeyDown(int, KeyEvent) Called when a new key event occurs.
onKeyUp(int, KeyEvent) Called when a key up event occurs.
onTrackballEvent(MotionEvent) Called when a trackball motion event occurs.
onTouchEvent(MotionEvent) Called when a touch screen motion event occurs.
Focus onFocusChanged(boolean, int, Rect) Called when the view gains or loses focus.
onWindowFocusChanged(boolean) Called when the window containing the view gains or loses focus.
Attaching onAttachedToWindow() Called when the view is attached to a window.
onDetachedFromWindow() Called when the view is detached from its window.
onWindowVisibilityChanged(int) Called when the visibility of the window containing the view has changed.

コールバック

メソッド 説明
onKeyDown() Called when a key down event has occurred.
onKeyLongPress() Called when a long press has occurred.
onKeyMultiple() Called when multiple down/up pairs of the same key have occurred in a row.
onKeyUp() Called when a key up event has occurred.
KeyEvent.Callback | Android Developers

構文

boolean onKeyDown(int keyCode, KeyEvent event)
boolean onKeyLongPress(int keyCode, KeyEvent event)
boolean onKeyMultiple(int keyCode, int count, KeyEvent event)
boolean onKeyUp(int keyCode, KeyEvent event)

イベントの解説

タッチモード (Touch Mode)

Androidの入力モードには、

  • キーパッドを使用するモード
  • スクリーンをタッチして操作するタッチモード

の2種類があります。タッチモードへは、ユーザーがスクリーンをタッチすることにより切り替わります。

タッチモードにおいてビューにフォーカスできるかどうかは、isFocusableInTouchMode()で調べられます。

アニメーション (Animation)

Animationオブジェクト。Animation | Android Developers

Androidの情報サイトから、まとめて検索