您可以通过获取当前可见性并切换它来使按钮设置其可见性.
button_food = (Button) findViewById(R.id.foodicon_layout);
button_travel = (Button) findViewById(R.id.travelicon_layout);
button_fuel = (Button) findViewById(R.id.fuelicon_layout);
button_fetch = (Button) findViewById(R.id.fetchicon_layout);
button_travel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int visibility = button_food.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE;
// Perform action on click
button_food.setVisibility(visibility);
button_fuel.setVisibility(visibility);
button_fetch.setVisibility(visibility);
}
});
像这样写它只是编写if语句的一种简单方法
int visibility;
if(button_food.getVisibility() == View.VISIBLE){
visibility = View.GONE;
} else {
visibility = View.VISIBLE;
}