如何设置浮动操作按钮

时间:2016-03-26 13:51:28

标签: android android-studio floating-action-button

我正在尝试学习循环浮动操作按钮,但我在Builder中遇到错误这里是我设置浮动操作按钮的代码。我已经采用了编译时依赖性。如何修复错误?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    // in Activity Context
    ImageView icon = new ImageView(this);
    icon.setImageResource(R.drawable.button_sub_action);
    FloatingActionButton actionButton = new FloatingActionButton.Builder(this)
            .setContentView(icon)
            .build();
    ImageView icon1 = new ImageView(this);
    icon1.setImageResource(R.drawable.button_action_dark_selector);
    ImageView icon2 = new ImageView(this);
    icon2.setImageResource(R.drawable.button_action_dark_selector);
    ImageView icon3 = new ImageView(this);
    icon3.setImageResource(R.drawable.button_action_dark_selector);

    SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this);

    SubActionButton button1 = itemBuilder.setContentView(icon1).build();
    SubActionButton button2 = itemBuilder.setContentView(icon2).build();
    SubActionButton button3 = itemBuilder.setContentView(icon3).build();

    FloatingActionMenu actionMenu = new FloatingActionMenu.Builder(this)
            .addSubActionView(button1)
            .addSubActionView(button2)
            .addSubActionView(button3)
            .attachTo(actionButton)
            .build();

}

1 个答案:

答案 0 :(得分:0)

      SubActionButton button1 = itemBuilder.setContentView(icon1).build();
      SubActionButton button2 = itemBuilder.setContentView(icon2).build();
      SubActionButton button3 = itemBuilder.setContentView(icon3).build();

按钮后添加此代码。

      button1.setOnClickListener(this);
      button2.setOnClickListener(this);
      button3.setOnClickListener(this)