2

〜100個のボタンが必要になるクラスがあります。次のようにコーディングできることはわかっています。

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/scroll"
    android:fillViewport="true"
    android:scrollbars="none"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="10dp"
    android:paddingBottom="65dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:background="@drawable/scrollviewborder"
        android:fillViewport="true" >

        <Button 
            android:id="@+id/b1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b2"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b3"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b4"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b5"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b6"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b7"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b8"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b9"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b10"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b11"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b12"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b13"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b14"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b15"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b16"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b17"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b18"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b19"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 

        <Button 
            android:id="@+id/b20"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:textSize="18sp" /> 
    </LinearLayout>
</ScrollView>

しかし、それはあまりパフォーマンス効率が良くないように思えます。もしこれを続けていれば、すぐにコード行が多すぎるという警告が表示されることはわかっています。これを行うよりエレガントな方法はありますか?XML または Java で実行できますが、どちらの方法でもかまいません。

編集

私のデザインに 100 個のボタンがあることについて騒ぎを起こしました。理由を説明しましょう。

Android/Apple アプリの Candy Crush Saga をプレイしている人はいますか? 一度に 1 つずつロックを解除してプレイする 400 以上のレベルがあることを知っていますか? それは私がやっていることと似ているので、非常に多くのボタンが必要です。

その説明の後、私がまだこれを間違っている場合は、私がまだ初心者の Android プログラマであるため、お知らせください。

4

4 に答える 4

3

次のようなことを試してください:

for (int i=0; i<100; i++) {
    Button button = (Button) getLayoutInflater().inflate(R.layout.button_view, null);
    // Do stuff to your button, add onClickListeners, etc.
    rootView.addView(button);
}

rootView以前にインスタンス化したLinearLayoutまたは類似の はどこにありますか。

そしてres/layout/button_view.xml次のようなものです:

<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
于 2013-06-14T13:27:16.947 に答える
0

Javaでそれを行う方がはるかに適切です。

LinearLayout lay = (LinearLayout) findViewById(R.id.your_liner_layout); // Get the liner layout

  for (i=0; i <= 100; i++) {
     Button btn = new Button(this); // Create a button

     btn.setGravity(Gravity.CENTER); // setting the gravity

     btn.setText("button number " + i); //setting the text

     LinearLayout.LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1); // Creating a LayoutParams : first args is the width, seconds the height and the last one the weight.

     lay.addView(btn, layout); // Adding the button to the linear layout
}
于 2013-06-14T13:34:20.630 に答える
0

まあ、これだけ多くのボタンが必要な場合は、どうしようもありません。コードを短くしたい場合は、Java コードにループを含めることができますが、個人的には、IDE でレイアウトを確認できる XML バージョンの方が好みです。

私が価値があると思う唯一のことは、これらのボタンの新しいスタイルを作成することです。それらはすべて 18sp のテキスト サイズを持ち、後で他の属性を追加することができます。

また、layout_gravity="center" と layout_weight=1 を削除してください。これらはあなたのケースでは効果がありません。

また、fill_parent を使用せず、match_parent を使用してください。

于 2013-06-14T13:30:45.817 に答える