如何以编程方式向片段添加按钮

时间:2014-07-17 15:40:41

标签: android android-fragments

我无法以编程方式将按钮添加到片段中。 (按钮数量可变)

我尝试使用“an”addView方法将按钮添加到rootView,但没有一个。

我尝试在按钮之前将按钮添加到布局中,但我不知道如何获得布局:(类型ID错误的预期资源)

RelativeLayout rl = getView().findViewById(R.layout.fragment_main);

但是当inflater膨胀时,资源就很好。

我尝试在“onViewCreated”方法中添加按钮,但我遇到了与findViewById(R.layout.fragment_main)相同的问题,告诉我存在错误(类型为id的预期资源)。

当我创建一个这样的按钮时:

Button bt = new Button(this);

“this”不是有效的上下文。不知道该用什么。

那么,我该如何实现呢?

基本代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
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);
        return rootView;
    }
}
}

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame" />

Fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

在您的代码中,要在fragment_main.xml上修改一些内容:

如果要访问xml上的元素,则需要为其指定唯一ID。如果您的相对布局缺少它,那么您将无法在片段视图中找到它。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_main_layout"     <!--some id-->
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

现在在你的片段类中你可以找到它!

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

    //Find the layout with the id you gave in on the xml
    RelativeLayout rl = (RelativeLayout) rootView.findViewById(R.id.fragment_main_layout);

   //And now you can add the buttons you need, because it's a fragment, use getActivity() as context
   Button bt = new Button(getActivity());
   //You can add LayoutParams to put the button where you want it and the just add it
   rl.addView(bt);


    return rootView;
}

就是这样,你的布局上有了一个新按钮

答案 1 :(得分:0)

  

我尝试在按钮之前将按钮添加到布局中,但我不知道如何获得布局:(类型ID错误的预期资源)

您的代码应该将其转换为相对布局并查找资源ID而不是布局文件。

RelativeLayout rl = (RelativeLayout) getView().findViewById(R.id.fragment_main_layout);

希望这有帮助

答案 2 :(得分:0)

您可以使用视图的上下文创建按钮:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
     View view = inflater.Inflate(Resource.Layout.fragment_question_list, container, false);

     Button btn = new Button(view.Context);

     return view
}