【问题标题】:How to add new Controls (Buttons, TextViews) to a Fragment dynamically如何动态地将新控件(按钮、文本视图)添加到片段
【发布时间】:2012-08-15 16:55:56
【问题描述】:

这是我的片段类:

public class FirstFragment extends Fragment {           
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {        
        EditText newText = new EditText(getActivity());

        newText.setText("This is a fragment");
        newText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
                                LayoutParams.WRAP_CONTENT));
        return inflater.inflate(R.layout.fragment_first, container, false); 
     }
}

我正在尝试将newText 添加到正在创建的片段中。

【问题讨论】:

    标签: java android controls


    【解决方案1】:

    只需存储从inflater.inflate 调用返回的视图,然后添加您想要的任何内容。

    最后,返回存储的视图。

    【讨论】:

      猜你喜欢
      • 2010-09-29
      • 2018-10-17
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多