Android
 Computer >> コンピューター >  >> プログラミング >> Android

制約レイアウトはAndroidでどのように機能しますか?


簡単に言うと、制約レイアウトは相対レイアウトの高度なバージョンです。子ビューの階層を減らし、パフォーマンスを向上させるために使用されます。

以下に示す制約レイアウトのプロパティ-

制約レイアウトはAndroidでどのように機能しますか?

コンテンツをラップする –データに従ってビューサイズをラップします。 制約レイアウトはAndroidでどのように機能しますか?

任意のサイズ –これは一致する親と非常によく似ています。 制約レイアウトはAndroidでどのように機能しますか?

固定サイズ –これにより、標準の高さと幅(固定サイズ)が可能になります。 制約レイアウトはAndroidでどのように機能しますか?

上記の例では、すべてのプロパティを含むボタンを示しました。次に、以下に示すようにコードレベルを調べます-

<Button
   android:layout_width="wrap_content"
   android:layout_height="53dp"
   android:layout_marginStart="8dp"
   android:layout_marginTop="8dp"
   android:layout_marginEnd="8dp"
   android:layout_marginBottom="8dp"
   android:text="Button"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintHorizontal_bias="0.391"
   app:layout_constraintStart_toStartOf="@+id/data1"
   app:layout_constraintTop_toTopOf="parent" />

上記では、レイアウトマージン(上、下、開始、終了)を宣言しています。これらは標準の距離です(親と一致するのと同様)

制約レイアウトでは、水平方向と垂直方向の2つのバイアスを使用できます。水平方向と垂直方向の重力空間。

[表示]をクリックすると、ビューに接続されているすべての階層が表示されます。

[表示]をクリックすると、[X]色のアイコンボタンが表示され、他のビューまたは親ビューとの階層を削除できます。

この例は、Androidで制約レイアウトを使用する方法を示しています。

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout / activity_main.xml

に追加します
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout  xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:app="https://schemas.android.com/apk/res-auto"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:id="@+id/layout"
   android:gravity="center"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <TextView
      android:id="@+id/data"
      android:layout_width="wrap_content"
      android:gravity="center"
      android:textSize="30sp"
      android:text="www.tutorialspoint.com"
      android:layout_height="wrap_content" />
   <TextView
      android:id="@+id/data1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      android:layout_marginTop="80dp"
      android:layout_marginEnd="8dp"
      android:gravity="center"
      android:text="www.tutorialspoint.com"
      android:textSize="30sp"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
   <Button
      android:layout_width="wrap_content"
      android:layout_height="53dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:layout_marginEnd="8dp"
      android:layout_marginBottom="8dp"
      android:text="Button"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintHorizontal_bias="0.43"
      app:layout_constraintStart_toStartOf="@+id/data1"
      app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、[アイコンの実行]をクリックします ツールバーから制約レイアウトはAndroidでどのように機能しますか? 。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

制約レイアウトはAndroidでどのように機能しますか?


  1. ListViewのリサイクルメカニズムはAndroidでどのように機能しますか?

    この例は、ListViewのリサイクルメカニズムがAndroidでどのように機能するかを示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo

  2. AndroidでsnackBarのレイアウトをカスタマイズする方法は?

    この例は、AndroidでsnackBarのレイアウトをカスタマイズする方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="htt