1

メイン レイアウト アクティビティ (3 行 x 5 列に配置) でこのレイアウトを 15 回インフレートし、各 ButtonpadButtonインスタンスに OnTouchListener を設定する必要があります。レイアウトを膨らませようとしましたが、リスナーを分離して設定する考えがありません...

Drumpad.xml を拡張するためのレイアウト

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/padLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pad"
    android:orientation="vertical"
    android:padding="3dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|clip_horizontal|top"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/padButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/titles2" />

        <Button
            android:id="@+id/padSelect"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="15dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="21dp" >

        <Button
            android:id="@+id/padName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:background="#0f0f0f"
            android:padding="2dp"
            android:text="@string/pad"
            android:textColor="#dfdfdf"
            android:textSize="10sp" />

    </LinearLayout>

</LinearLayout>

メインのレイアウト (コンテナ) activity_drum.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/DrumLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#999"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".Drum" >

</LinearLayout>
4

1 に答える 1

1

同じIDのボタンを複数持つことができるかどうかはわかりません。ただし、ルート ビューを取得して、ViewGroup. とを使用getChildCount()getChildAt()、必要に応じて再帰します。このような:

//we start from the root view
ViewGroup rootViewGroup = findViewById(R.id.rootLinearLayout);

for (int i = 0; i < this.getChildCount(); i++) {
    View childView = this.getChildAt(i);

    //is it a button?
    if (childView instanceof Button) {
        Button button = (Button) childView;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //insert here the code
            }
        });
    }

    if (childView instanceof ViewGroup) {
        //iterate throught childView children
    }
}

わかりました、このコードを取得して、少し苦労せずに動作させることはできません。それは単なる出発点です。しかし...

アプローチを再考し、独自のカスタム ビューを開発する必要があると思います。複合ビューを使用します。こちらをご覧ください。

http://developer.android.com/guide/topics/ui/custom-components.html#compound

少し練習が必要ですが、これはあなたの目的には最適だと思います。

編集

たぶん、ここでいくつかの有用な情報を見つけることができます:

Android アプリケーションの GridView にボタンの配列を追加する

そしてここ:

http://developer.android.com/guide/tutorials/views/hello-gridview.html

そしてここ(ドラムマシン):

http://mindtherobot.com/blog/420/android-beatz-making-a-drum-machine-app/

于 2013-03-19T19:46:35.883 に答える