Android-imagebutton-control
Android-ImageButtonコントロール
'_ImageButtonは、子の正確な場所を指定できるAbsoluteLayoutです。 これは、ユーザーが押したりクリックしたりできる(テキストではなく)画像のあるボタンを示しています。_
Androidボタンスタイルセット
ImageButtonの属性
以下は、ImageButtonコントロールに関連する重要な属性です。 属性の完全なリストと、これらの属性を実行時に変更するために使用できる関連メソッドについては、Androidの公式ドキュメントを確認してください。
*android.widget.ImageView* クラスから継承-
Sr.No | Attribute & Description |
---|---|
1 |
android:adjustViewBounds ImageViewの境界を調整してそのドロアブルのアスペクト比を保持する場合は、これをtrueに設定します。 |
2 |
android:baseline これは、このビュー内のベースラインのオフセットです。 |
3 |
android:baselineAlignBottom trueの場合、画像ビューはその下端に基づいてベースラインに揃えられます。 |
4 |
android:cropToPadding trueの場合、画像はパディングに収まるようにトリミングされます。 |
5 |
android:src これにより、このImageViewのコンテンツとしてドロアブルが設定されます。 |
*android.view.View* クラスから継承-
Sr.No | Attribute & Description |
---|---|
1 |
android:background これは背景として使用するドロアブルです。 |
2 |
android:contentDescription これは、ビューのコンテンツを簡単に説明するテキストを定義します。 |
3 |
android:id これは、このビューの識別子名を提供します |
4 |
android:onClick これは、ビューがクリックされたときに呼び出すこのビューのコンテキスト内のメソッドの名前です。 |
5 |
android:visibility これにより、ビューの初期可視性が制御されます。 |
例
この例では、線形レイアウトとImageButtonを使用して独自のAndroidアプリケーションを作成する方法を示す簡単な手順を紹介します。
Step | Description |
---|---|
1 | You will use Android studio IDE to create an Android application and name it as myapplication under a package com.example.myapplication as explained in the Hello World Example chapter. |
2 | Modify src/MainActivity.java file to add a click event. |
3 | Modify the default content of res/layout/activity_main.xml file to include Android UI control. |
4 | No need to define default constants in android, Android studio takes care of default constants. |
5 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/com.example.myapplication/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。
'_以下の例では、abcはfinddevguidesのイメージを示しています_
package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageButton imgButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgButton =(ImageButton)findViewById(R.id.imageButton);
imgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"You download is
resumed",Toast.LENGTH_LONG).show();
}
});
}
}
以下は res/layout/activity_main.xml ファイルの内容です-
<?xml version="1.0" encoding="utf-8"?>
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView android:text="Tutorials Point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/abc"/>
</RelativeLayout>
以下は、これらの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">myapplication</string>
</resources>
以下は、 AndroidManifest.xml のデフォルトのコンテンツです-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapplication.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
*myapplication* アプリケーションを実行してみましょう。 環境設定中に *AVD* を作成したと思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 Android StudioはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
ImageButtonをクリックすると、次の画面が表示されます。トーストメッセージが表示されます。
運動
プログラミング時にレイアウトXMLファイルのImageButtonの異なる属性を使用して上記の例を試して、ImageButtonのルックアンドフィールを変更することをお勧めします。 編集可能にし、フォントの色、フォントファミリ、幅、textSizeなどに変更して、結果を確認してください。 1つのアクティビティで複数のImageButtonコントロールを使用して上記の例を試すこともできます。