ボタンが機能するためにxmlファイルは必要ありません。ドローアブルのpngファイルは画像専用です。ボタンはプログラムまたはxmlで作成できますが、Button
インスタンスはに使用されるものでありonClick()
、必ずしもxmlである必要はないため、どこかにボタンを作成する必要があります。Layout
どちらの方法でも、使用するxmlファイルが必要です。setContentView()
これによりButton
、そのレイアウトファイルに配置するか、Javaコードで作成できますが、どちらの方法でも、を作成する必要があります。Button
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="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
これでButton
、背景を設定するか、ImageButton
代わりに使用して `android:src =" @ drawable/..."を設定できます。
次に、コードでボタンインスタンスを取得する必要があります
ボタンbtn1=(ボタン)findViewById(R.id.button)
レイアウトを次のように呼び出した後
setContentView(R.layout.your_layout_file);
あなたがちょうどあなたが望むように聞こえるものとImageButton
交換するだけButton
を使用する場合。ImageButton
これがあなたにとってもう少し意味をなすのに役立つことを願っています