我一直在尝试动态地向我的片段添加按钮,但我尝试过的所有方法都不起作用。
这些是我尝试的一些方法:
1
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
LinearLayout linearlayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linearlayout.setLayoutParams(buttonParams);
linearlayout.setOrientation(LinearLayout.HORIZONTAL);
Button button = new Button(getActivity());
button.setLayoutParams(buttonParams);
button.setText("????????????????????");
button.setTextSize(16);
Button button2 = new Button(getActivity());
button2.setLayoutParams(buttonParams);
button2.setText("!!!!!!!!!!!!!!!!!!");
button2.setTextSize(64);
linearlayout.addView(button);
linearlayout.addView(button2);
container.addView(linearlayout);
View myView = inflater.inflate(R.layout.fragment_general_layout, container, false);
return myView;
}
这将为我提供以下屏幕截图First method。 我真的不喜欢这个,因为这会在Activity上创建一个按钮,它会出现在我使用相同Activity的其他Fragment上。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_general_layout, container, false);
for (int i = 0; i < ArrayOfNames.length; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(myView.getContext());
btn.setId(i);
final int id_ = btn.getId();
btn.setText(ArrayOfNames[i]);
btn.setBackgroundColor(Color.CYAN);
btn.setLayoutParams(params);
linearlayout.addView(btn, params);
btn = myView.findViewById(id_);
btn.setVisibility(View.VISIBLE);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do smth
}
});
return myView;
}
我假设这个方法会生成按钮(因为代码运行正常,并且在设置OnClickListner()时,至少按钮没有空指针引用。但我看不到实际按钮我的屏幕。此外,无法点击它。
我试图引用这些 网址:Programmatically add buttons to a Fragment
Adding buttons programmatically to a fragment
How to add a button programmatically to a fragment
任何帮助都将非常感激。
答案 0 :(得分:2)
onCreateView
方法希望您返回正在膨胀的视图。您的代码很好,但是您将按钮添加到错误的视图中。试试这个:
//container.addView(linearlayout);
myView = inflater.inflate(R.layout.fragment_general_layout, container, false);
myView.addView(linearlayout);
这应该将您的按钮添加到您的视图中。我假设您的R.layout.fragment_general_layout
是LinearLayout
orientation="vertical"
答案 1 :(得分:1)
最后,我已经做好了这件事。第二种方法有问题,因为没有
btn = myView.findViewById(id_);
。
这将指向null,虽然我已将我的按钮的ID设置为我所述的值。使用此方法,通过设置单独的onClick()
方法,很容易获得按钮的值。
for (int i = 0; i < ArrayOfNames.length; i++) {
final Button myButton = new Button(myView.getContext());
myButton.setText(RAnames[i]);
myButton.setId(i + 1);
myButton.setOnClickListener(this);
myButton.setBackgroundColor(getResources().getColor(R.color.colorAccent));
myButton.setTextSize(18);
myButton.setPadding(20, 0, 20, 0);
LinearLayout linearlayout = (LinearLayout) myView.findViewById(R.id.btnholder);
linearlayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0, 0, 0, 10);
linearlayout.addView(myButton, buttonParams);
}