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

Android用語集

カテゴリー: レイアウト  閲覧数:494 配信日:2014-04-01 06:50


サイズ


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 内部の要素の位置