2017-06-21 134 views
0

我正在創建一個android佈局,我將在其中放置3個按鈕,並在它們之間留有一些空間。但根據要求,按鈕數可降至2,並且需要根據可用屏幕空間重新定位按鈕。請參閱下面的屏幕。如何根據條件在佈局上放置按鈕?

3 buttons

現在有兩個按鈕第二種情況應該是這樣

2 buttons

我應該如何去這樣做呢?

回答

0

你可以試試下面的代碼實現根據你的病情同樣添加按鈕佈局

//the layout on which you are working 
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags); 

//set the properties for button 
Button btnTag = new Button(this); 
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
btnTag.setText("Button"); 
btnTag.setId(some_random_id); 

//add button to the layout 
layout.addView(btnTag); 
0

您可以定義在佈局XML中的所有三個按鈕,然後:

,如果你要顯示的第三個按鈕

thirdButton.setVisibility(View.VISIBLE); 

,如果你想隱藏

thirdButton.setVisibility(View.GONE); 

至少如果使用線性或相對佈局,它應該自己重新定位。

1

只需將根據您的病情就像按鈕的知名度以下幾點:

button.setVisibility(View.GONE); 

當您設置可視性,然後走了空間將視爲可用於其他佈局和您的將被設置爲按您的慾望。

當您將可見性設置爲不可見時,該空間將被視爲已分配。

+0

@Backspace可以接受答案嗎? –

相關問題