使用代码创建布局

时间:2014-04-19 16:30:45

标签: android

我在使用代码创建布局时遇到问题。我只想在线性布局上添加一个按钮对象,但它不会创建任何按钮。

这是我的onCreate方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

    Button button = new Button(this);
    button.setText(R.string.click_me);

    layout.addView(button);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

我的fragment_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:id="@+id/layout"
android:orientation="vertical" >

***更新1 ***

因此,当您拥有片段xml时,在布局上添加对象时,它必须位于片段类上。感谢@Fllo指出这一点。避风港没想到这样。 :(

这是我在PlaceHolderFragment类中添加按钮的更新代码。

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.layout);
        Button button = new Button(getActivity());
        button.setText(R.string.click_me);
        layout.addView(button);
        return rootView;
    }
}

3 个答案:

答案 0 :(得分:0)

您似乎在 fragment_main.xml 而非 activity_main.xml 中构建了视图。

当你第一次创建一个新的android项目时,你会自动创建和打开这些文件:

enter image description here

然后,当您开始时,在fragment_main.xml文件中添加视图(例如:TextView),而您尝试在Activity内使用此视图执行基本事件它使用这种布局:

setContentView(R.layout.activity_main); // using the activity_main.xml file

您无法运行自己的应用,或者有时只有白屏,因为您尝试调用/显示的视图布局错误。

  

解决方案:将onCreateView方法中的所有内容移动到Fragment类中。调用视图并在相关片段中执行某些操作,而不是父活动


onCreateView方法中的所有内容移到PlaceholderFragment课程中,如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);  

    // Don't forget to attach your view to the layout
    LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.layout);
    // Attach the context
    Button button = new Button(getActivity());
    // Add the layout params
    button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    // Set the text
    button.setText(R.string.click_me);
    // Add the view
    layout.addView(button);  

    return rootView;
}

答案 1 :(得分:0)

尝试添加此行。

button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

答案 2 :(得分:0)

尝试添加按钮,如下所示:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout.addView(button, params);