Android-linear-layout
Androidリニアレイアウト
'_Android LinearLayoutは、_vertically_または_horizontally_のすべての子を整列させるビューグループです。_
線形レイアウト
LinearLayoutの属性
以下は、LinearLayoutに固有の重要な属性です-
Sr.No | Attribute & Description |
---|---|
1 |
android:id これは、レイアウトを一意に識別するIDです。 |
2 |
android:baselineAligned これは「true」または「false」のブール値でなければならず、レイアウトがその子のベースラインを揃えないようにします。 |
3 |
android:baselineAlignedChildIndex 線形レイアウトがベースラインに揃えられた別のレイアウトの一部である場合、ベースラインに揃える子を指定できます。 |
4 |
android:divider これは、ボタン間の垂直分割線として使用するために描画可能です。 「#rgb」、「#argb」、「#rrggbb」、または「#aarrggbb」の形式の色の値を使用します。 |
5 |
android:gravity これは、X軸とY軸の両方でオブジェクトがコンテンツを配置する方法を指定します。 可能な値は、top、bottom、left、right、center、center_vertical、center_horizontalなどです。 |
6 |
android:orientation これは配置の方向を指定し、行には「水平」、列には「垂直」を使用します。 デフォルトは水平です。 |
7 |
android:weightSum 子供の体重の合計 |
例
この例では、簡単な手順で、リニアレイアウトを使用して独自のAndroidアプリケーションを作成する方法を示します。 _Hello World Example_の章で作成したAndroidアプリケーションを変更するには、次の手順に従います-
Step | Description |
---|---|
1 | You will use Android Studio to create an Android application and name it as Demo under a package com.example.demo as explained in the Hello World Example chapter. |
2 | Modify the default content of res/layout/activity_main.xml file to include few buttons in linear layout. |
3 | No need to change string Constants.Android studio takes care of default strings |
4 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/com.example.demo/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
以下は res/layout/activity_main.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" >
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>
変更したばかりの* Hello World!アプリケーションを実行してみましょう。 環境設定中に *AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
それでは、レイアウトの方向を android:orientation = "horizontal" に変更して、同じアプリケーションを実行してみましょう。次の画面が表示されます-