画面を構成する要素 View

レイアウト

Viewとは?

 状態:-  閲覧数:1,294  投稿日:2013-05-28  更新日:2014-04-07  
レイアウトとウィジェットをまとめて、View と呼ぶ

1.レイアウト(Layout)
・LinearLayout、RelativeLayout、FrameLayout

2.ウィジェット(Widget)
・TextView、EditText、ImageView、Button、CheckBox、RadioButton

レイアウト関連主要クラスの階層一覧

2.ウィジェット(Widget)の種類

 閲覧数:431 投稿日:2013-05-28 更新日:2014-03-28 

Widgetの種類


・Androidが標準でもつWidget
名称 XML要素名(クラス名)
テキスト TextView
編集可能テキスト EditView
自動保管テキスト AutoCompleteTextView
画像 ImageView
ボタン Button
イメージボタン ImageButton
チェックボックス CheckBox
ラジオボタン/ラジオグループ RadioButton/RadioGroup
トグルボタン ToggleButton
日付選択 DatePicker
時刻選択 TimePicker
アナログ時計 AnalogClock
デジタル時計 DigitalClock
タイマー SystemClock
プログレスバー ProgressBar
ズームスライダー ZoomControls


View を配置する際に使用する各種パラメータ

 閲覧数:493 投稿日:2014-04-01 更新日:2014-04-08 

サイズ


ViewGroup.LayoutParamsクラス
XML属性名 内容
android:layout_height Viewの高さ
android:layout_width Viewの幅
<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"
   tools:context=".MainActivity" >

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_centerVertical="true"
       android:text="hello_world" />

</RelativeLayout>



余白


margin(マージン)、padding(パディング)を使用することで、Viewの余白を設定することが可能
・マージン … Viewの外側余白を調整
・パディング … Viewの内側余白を調整

ViewGroup.MarginLayoutParamsクラス
要素 意味
android:layout_margin 上下左右のマージン
android:layout_marginTop 上のマージン
android:layout_marginBottom 上下のマージン
android:layout_marginLeft 左のマージン
android:layout_marginRight 右のマージン
Viewクラス
要素 意味
android:padding 上下左右のパディング
android:paddingTop 上のパディング
android:paddingBottom 下のパディング
android:paddingLeft 左のパディング
android:paddingRight 右のパディング
    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="12dp"
       android:background="#f2f2f2"
       android:text="Margin"
       android:textSize="30sp" />

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="#f2f2f2"
       android:padding="12dp"
       android:text="Padding"
       android:textSize="30sp" />



単位


・Androidには様々な画面サイズ、画面密度(解像度)の端末が存在
・そのためpx(pixel)でサイズ指定をすると端末によって画像が小さくなったり大きくなったりしてしまう
・Androidではこのような複数解像度の端末に対応するため以下の単位が用意されている

dp(dip)
・density-independent pixels (密度非依存のピクセル) の略
・160dpi(dots per inch) の画面を基準とされており、1dipは160dpiの画面では1ピクセルと同等と定義されている
・式にすると以下のようになり、解像度に対する1dipが何pxに相当するかがわかる
px = dp * (dpi / 160)

・320dpiの画面の場合だと1dipは2pxに自動的に換算され、画面上に反映される
・よって、サイズにdpを使用することで特に意識することなく複数のが解像度端末に対応することが可能

※dipとdpは両方使えるがdpの記述の方がspと統一感があるのでわかりやすい

sp
・scale-independent pixels(スケール非依存のピクセル)の略。
・指定したサイズは、解像度とユーザーが設定したフォントサイズにあわせて自動的にスケールされる

文字の大きさ


位置


FrameLayout.LayoutParamsクラス
XML属性名 内容
android:layout_gravity 自分の位置
LinearLayoutクラス RelativeLayoutクラス
XML属性名 内容
android:gravity 内部の要素の位置



レイアウト

レイアウト定義。Viewを配置する方法は2種類

コメント投稿(ログインが必要)