【问题标题】:Android: How to programatically create multiple imagebutton views?Android:如何以编程方式创建多个图像按钮视图?
【发布时间】:2012-06-04 09:24:18
【问题描述】:

我有超过 30 个可点击的图像按钮视图,我想以编程方式创建它们,而不是使用多个 xml 文件。有人可以解释一下我该怎么做吗?对于每个图像按钮,我需要:

  1. 能够以编程方式命名和设置每个视图的 ID,例如 R.drawable.button01、R.drawable.button02 等,因为稍后我需要使用 findViewById() 来引用它们。

  2. 每个图像按钮都是可点击的,我为每个按钮都有一个按下按钮的图像,所以我需要弄清楚如何将按钮按下动作动态分配给每个按钮,而不是为每个按钮使用一个 xml 资源文件。

提前致谢。

【问题讨论】:

    标签: android android-layout imagebutton


    【解决方案1】:

    将任何布局作为根,如线性或相对初始化它..

    LinearLayout layout = new LinearLayout(context);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    ImageButton button =new ImageButton(context);
    layout.addView(layout);
    setContentView(layout);
    

    我希望这能解决你的问题。

    【讨论】:

    • 这并没有解释如何做 1 和 2.. 我知道如何以编程方式创建图像按钮,但我希​​望能够定义和设置资源 ID 并以编程方式定义点击操作.. .
    • 要设置 id,只需一个函数 setId(),它接受整数作为 id,您也可以设置点击监听器。
    • 好的。谢谢。这有助于构建图像按钮。但是如何定义按钮按下的图像变化?如前所述,我不想使用 xml 来定义每个按钮的按钮按下和正常状态资源文件。
    • 您可以使用 isPressed() ,setPressed 方法从视图类继承的按钮,并根据您的选择设置drawable。
    • 谢谢。我让它工作了。对于那些感兴趣的人,这是我的代码:StateListDrawable states = new StateListDrawable(); states.addState(new int[] {-android.R.attr.state_pressed},getResources().getDrawable(idNormal)); // Note the "-" states.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(idClick)); btnMapLoc.setImageDrawable(states);
    【解决方案2】:

    视图有一个函数setId(),您可以使用它来设置一个ID。图片可以使用setImageResource()

    【讨论】:

      【解决方案3】:

      对于我的项目,我正在为学校创建一个骰子游戏 (Yahtzee)。对于我的按钮,我将它们添加到 XML 中

      <ImageButton
              android:id = "@+id/dice1"
              android:src = "@drawable/red_1_dice"
              android:layout_width = "wrap_content"
              android:layout_height = "wrap_content"
              android:layout_marginTop = "60px"
              android:layout_marginLeft = "30px" />
      
          <ImageButton
              android:id = "@+id/dice2"
              android:src = "@drawable/red_2_dice"
              android:layout_width = "wrap_content"
              android:layout_height = "wrap_content"
              android:layout_marginLeft = "250px" 
              android:layout_marginTop = "-130px"/>
      
          <ImageButton
              android:id = "@+id/dice3"
              android:src = "@drawable/red_3_dice"
              android:layout_width = "wrap_content"
              android:layout_height = "wrap_content"
              android:layout_marginLeft = "135px"
              android:layout_marginTop = "20px" />
      

      然后我主要是这样做的。

      public class Z_veselinovic_yahtzeeActivity extends Activity 
      {
          /** Called when the activity is first created. */
      
          ImageButton button1, button2, button3, button4, button5;
          Button start, reroll, hold;
      
          @Override
          public void onCreate(Bundle savedInstanceState) 
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
      
              Buttons();
          }
      
      
          public void Buttons()
          {
      
      
              button1 = (ImageButton)findViewById(R.id.dice1);
              button2 = (ImageButton)findViewById(R.id.dice2);
              button3 = (ImageButton)findViewById(R.id.dice3);
              button4 = (ImageButton)findViewById(R.id.dice4);
              button5 = (ImageButton)findViewById(R.id.dice5);
      
              start = (Button)findViewById(R.id.Start);
              reroll = (Button)findViewById(R.id.Reroll);
              hold = (Button)findViewById(R.id.Hold);
      
              reroll.setVisibility(View.GONE);
              hold.setVisibility(View.GONE);
      
              start.setOnClickListener(new OnClickListener()
              {
                  public void onClick(View whatever)
                  {
      
                      Toast.makeText(getBaseContext(), start.getText() + " Game", Toast.LENGTH_LONG).show();
      
                      Random rand1 = new Random();
                      Random rand2 = new Random();
                      Random rand3 = new Random();
                      Random rand4 = new Random();
                      Random rand5 = new Random();
      
                      int dice_num_1 = rand1.nextInt(6) + 1;
                      int dice_num_2 = rand2.nextInt(6) + 1;
                      int dice_num_3 = rand3.nextInt(6) + 1;
                      int dice_num_4 = rand4.nextInt(6) + 1;
                      int dice_num_5 = rand5.nextInt(6) + 1;
      
      
      
                      if(dice_num_1 == 1)
                      {
                          button1.setImageResource(R.drawable.red_1_dice);
                      }
      
                      else if(dice_num_1 == 2)
                      {
                          button1.setImageResource(R.drawable.red_2_dice);
                      }
      
                      else if(dice_num_1 == 3)
                      {
                          button1.setImageResource(R.drawable.red_3_dice);
                      }
      
                      else if(dice_num_1 == 4)
                      {
                          button1.setImageResource(R.drawable.red_4_dice);
                      }
      
                      else if(dice_num_1 == 5)
                      {
                          button1.setImageResource(R.drawable.red_5_dice);
                      }
      
                      else if(dice_num_1 == 6)
                      {
                          button1.setImageResource(R.drawable.red_6_dice);
                      }
      
      
      
      
                      if(dice_num_2 == 1)
                      {
                          button2.setImageResource(R.drawable.red_1_dice);
                      }
      
                      else if(dice_num_2 == 2)
                      {
                          button2.setImageResource(R.drawable.red_2_dice);
                      }
      
                      else if(dice_num_2 == 3)
                      {
                          button2.setImageResource(R.drawable.red_3_dice);
                      }
      
                      else if(dice_num_2 == 4)
                      {
                          button2.setImageResource(R.drawable.red_4_dice);
                      }
      
                      else if(dice_num_2 == 5)
                      {
                          button2.setImageResource(R.drawable.red_5_dice);
                      }
      
                      else if(dice_num_2 == 6)
                      {
                          button2.setImageResource(R.drawable.red_6_dice);
                      }
      

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多