0
我想知道如何添加或鏈接與findviewID一個LinearLayout與多個按鈕(編程添加)和它們之間的空間。我試圖在我的片段中創建一個XML佈局,並將其鏈接到LinearLayout類型的變量,然後使用LayoutParams工作,但我無法正常工作。我想知道這裏有沒有人有任何建議。下面是我的代碼。編程添加LinearLayout與按鈕之間的填充片段
private GuiLoader guiloader = new GuiLoader();
private LinearLayout layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.media_list_fragment, container, false);
layout = (LinearLayout)v.findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f);
param.setMargins(100, 100, 100, 100);
layout.setPadding(500, 86, 50, 50);
layout.setLayoutParams(param);
和按鈕創建一個通過量類
for (final VideoDevice videoDevice : videoDevices) {
Button myButton = guiloader.createButton(getActivity());
myButton.setBackgroundColor(getResources().getColor(R.color.green));
myButton.setText(videoDevice.description);
layout.addView(myButton, guiloader.buttonWidth, guiloader.buttonHeight);
提前感謝!
感謝您提出我的問題,當我將LinearLayout更改爲gridview時,我無法在for循環中使用它,並且將使用哪個按鈕命令設置邊距,因爲沒有列出。 –
您可以將這添加到您的for循環中: 'LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); param.setMargins(100,100,100,100); myButton.setLayoutParams(param);' – danwilkie