Android-user-interface-layouts

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

Android-UIレイアウト

ユーザーインターフェイスの基本的な構成要素は、Viewクラスから作成される View オブジェクトであり、画面上の四角形の領域を占有し、描画とイベント処理を担当します。 ビューは、ボタン、テキストフィールドなどの対話型UIコンポーネントを作成するために使用されるウィジェットの基本クラスです。

*ViewGroup* は *View* のサブクラスであり、他のビューまたは他のViewGroupを保持し、それらのレイアウトプロパティを定義する不可視のコンテナーを提供します。

3番目のレベルには、ViewGroupクラスのサブクラスであるさまざまなレイアウトがあり、典型的なレイアウトはAndroidユーザーインターフェイスの視覚構造を定義し、実行時に View/ViewGroup オブジェクトを使用して作成するか、単純なXMLを使用してレイアウトを宣言できますプロジェクトのres/layoutフォルダーにある main_layout.xml ファイル。

レイアウト

レイアウトパラメーター

このチュートリアルでは、XMLファイルで定義されたレイアウトに基づいてGUIを作成する方法について詳しく説明します。 レイアウトには、ボタン、ラベル、テキストボックスなど、あらゆるタイプのウィジェットを含めることができます。 以下は、LinearLayoutを持つXMLファイルの簡単な例です-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="This is a TextView"/>

   <Button android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="This is a Button"/>

   <!-- More GUI components go here  -->

</LinearLayout>

レイアウトが作成されたら、以下に示すように、_Activity.onCreate()_コールバック実装で、アプリケーションコードからレイアウトリソースをロードできます-

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
}

Androidのレイアウトタイプ

Androidが提供する多くのレイアウトは、ほぼすべてのAndroidアプリケーションで使用して、異なるビュー、ルックアンドフィールを提供します。

Sr.No Layout & Description
1

Linear Layout

LinearLayoutは、すべての子を単一の方向(垂直または水平)に整列させるビューグループです。

2

Relative Layout

RelativeLayoutは、相対的な位置に子ビューを表示するビューグループです。

3

Table Layout

TableLayoutは、ビューを行と列にグループ化するビューです。

4

Absolute Layout

AbsoluteLayoutを使用すると、子の正確な場所を指定できます。

5

Frame Layout

FrameLayoutは、単一のビューを表示するために使用できる画面上のプレースホルダーです。

6

List View

ListViewは、スクロール可能なアイテムのリストを表示するビューグループです。

7

Grid View

GridViewは、アイテムを2次元のスクロール可能なグリッドで表示するViewGroupです。

レイアウト属性

各レイアウトには、そのレイアウトの視覚的なプロパティを定義する属性のセットがあります。 すべてのレイアウトに共通の属性はほとんどなく、それらはそのレイアウトに固有のその他の属性です。 以下は共通の属性であり、すべてのレイアウトに適用されます。

Sr.No Attribute & Description
1

android:id

これは、ビューを一意に識別するIDです。

2

android:layout_width

これはレイアウトの幅です。

3

android:layout_height

これはレイアウトの高さです

4

android:layout_marginTop

これは、レイアウトの上部にある余分なスペースです。

5

android:layout_marginBottom

これは、レイアウトの下側の余分なスペースです。

6

android:layout_marginLeft

これは、レイアウトの左側の余分なスペースです。

7

android:layout_marginRight

これは、レイアウトの右側の余分なスペースです。

8

android:layout_gravity

これは、子ビューの配置方法を指定します。

9

android:layout_weight

これにより、レイアウト内の余分なスペースをビューに割り当てる量を指定します。

10

android:layout_x

これは、レイアウトのx座標を指定します。

11

android:layout_y

これは、レイアウトのy座標を指定します。

12

android:layout_width

これはレイアウトの幅です。

13

android:layout_width

これはレイアウトの幅です。

14

android:paddingLeft

これは、レイアウト用に埋められた左のパディングです。

15

android:paddingRight

これは、レイアウトに使用される正しいパディングです。

16

android:paddingTop

これは、レイアウトの上部のパディングです。

17

android:paddingBottom

これは、レイアウトの下部のパディングです。

ここで、幅と高さは、dp(密度に依存しないピクセル)、sp(スケールに依存しないピクセル)、pt(1/72インチのポイント)、px(ピクセル)、mm(ミリメートル)、最後にインチ(インチ)。

あなたは正確な測定で幅と高さを指定することができますが、多くの場合、これらの定数のいずれかを使用して幅または高さを設定します-

  • android:layout_width = wrap_content は、コンテンツに必要なサイズに合わせてビューのサイズを変更するようビューに指示します。
  • android:layout_width = fill_parent は、親ビューと同じ大きさになるようにビューに指示します。

重力属性は、ビューオブジェクトの配置に重要な役割を果たし、次の定数値の1つ以上(「|」で区切られた)を取ることができます。

Constant Value Description
top 0x30 Push object to the top of its container, not changing its size.
bottom 0x50 Push object to the bottom of its container, not changing its size.
left 0x03 Push object to the left of its container, not changing its size.
right 0x05 Push object to the right of its container, not changing its size.
center_vertical 0x10 Place object in the vertical center of its container, not changing its size.
fill_vertical 0x70 Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal 0x01 Place object in the horizontal center of its container, not changing its size.
fill_horizontal 0x07 Grow the horizontal size of the object if needed so it completely fills its container.
center 0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill 0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical 0x80 Additional option that can be set to have the top and/or bottom edges of the child clipped to its container’s bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
clip_horizontal 0x08 Additional option that can be set to have the left and/or right edges of the child clipped to its container’s bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
start 0x00800003 Push object to the beginning of its container, not changing its size.
end 0x00800005 Push object to the end of its container, not changing its size.

身元確認

ビューオブジェクトには、ツリー内でビューを一意に識別する一意のIDが割り当てられている場合があります。 XMLタグ内のIDの構文は-

android:id="@+id/my_button"

以下は、@および+記号の簡単な説明です-

  • 文字列の先頭のアットマーク(@)は、XMLパーサーがID文字列の残りを解析および展開し、IDリソースとして識別することを示します。
  • プラス記号(+)は、これが作成されてリソースに追加される必要がある新しいリソース名であることを意味します。 ビューオブジェクトのインスタンスを作成し、レイアウトからキャプチャするには、次を使用します-
Button myButton = (Button) findViewById(R.id.my_button);