这里的xml就像这样
<com.example.MyViewGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.example.MyViewGroup>
课程MyViewGroup
扩展ViewGroup
。
所以我的问题是可以以编程方式创建按钮并将其添加到视图中吗?
答案 0 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = new ContextThemeWrapper(getActivity(), R.style.Beam_Light);
LayoutInflater localInflater = inflater.cloneInContext(context);
final View root = localInflater.inflate(R.layout.sv_contact_info_fragment, container, false);
LinearLayout dynamicLayout = (LinearLayout) root.findViewById(R.id.beam_contact_info_fragment_lchild);
View view = localInflater.inflate(R.layout.sv_header, null);
dynamicLayout.addView(view);
}
答案 1 :(得分:0)
是的,这是可能的。您可以使用addView()方法
以下是一个简单的例子:
MyViewGroup mvg = (MyViewGroup) findViewById(R.id.screen);
Button btn = new Button(context);
btn.setText("My Button");
mvg.addView(btn);