嗨我創建了一個包含兩個按鈕添加和刪除的佈局,當我點擊加號按鈕時,它將添加一個微調器,並且我有另一個按鈕(刪除)可以在圖片中看到,現在我希望當我點擊刪除按鈕時,它會從最後刪除微調,請指導我如何刪除微調?我已經通過編程添加了微調,並且正在編程中創建微調。我會發布我的代碼。 動態地添加和刪除微調器
public class AddMenulist extends Fragment{
TextView tv1,tv2,tv3,tv4;
Button bt1 , bt2;
LinearLayout linearlayout,linearlayout1;
ArrayList<String> spinnerArray = new ArrayList<String>();
private Spinner spinner;
private Spinner spinner2;
public AddMenulist(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
tv1 = (TextView)rootView.findViewById(R.id.userName);
tv2 = (TextView)rootView.findViewById(R.id.userName2);
bt1 = (Button)rootView.findViewById(R.id.add);
bt2 = (Button)rootView.findViewById(R.id.delete);
linearlayout = (LinearLayout)rootView.findViewById(R.id.main_layout);
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
bt1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
//Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();
linearlayout1 = new LinearLayout(getActivity());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearlayout1.setOrientation(LinearLayout.HORIZONTAL);
layoutParams.setMargins(10, 0, 0, 0);
linearlayout1.setLayoutParams(layoutParams);
tv3 = new TextView(getActivity());
tv3.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tv3.setText("Label");
tv4 = new TextView(getActivity());
tv4.setText("Category");
spinner = new Spinner(getActivity());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner2 = new Spinner(getActivity());
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner2.setAdapter(spinnerArrayAdapter2);
linearlayout1.addView(tv3);
linearlayout1.addView(spinner);
linearlayout1.addView(tv4);
linearlayout1.addView(spinner2);
linearlayout.addView(linearlayout1);
}
});
bt2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
//Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
您沒有嘗試刪除功能?或目前它從第一個刪除? –
試試這個:'linearlayout1.removeView(view)' –
好吧,我會嘗試一個。 – Achin