android - 如何以编程方式将按钮添加到 fragment

标签 android android-fragments

我无法设法以编程方式向 fragment 添加按钮。 (按钮的数量是可变的)

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

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

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

但是当充气机给它充气时,资源很好。

我试图在“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>

最佳答案

在您的代码中,在 fragment_main.xml 中有几处需要更正:

如果你想访问一个xml上的元素,你需要给它一个唯一的id。在相对布局的情况下,您丢失了它,因此您将无法在 fragment View 中找到它。

<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>

现在在您的 fragment 类中您可以找到它!

@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;
}

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

关于android - 如何以编程方式将按钮添加到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24807982/

相关文章:

android - 单击返回时显示两个 fragment

java - 如何在另一个 Activity 的 fragment 中单击按钮时在另一个 Activity 中打开包含 webViews 的 fragment ?

android - 从 Activity 调用 getSupportFragmentManager() 时出错

android - 使用 Android 加载 1,440,000 大小的 ArrayList 时运行速度极慢

Android 下载管理器。COLUMN_TOTAL_SIZE_BYTES 在状态运行期间返回 -1

android - 在弹出窗口外单击时,如何关闭弹出窗口?

android - 从左侧滑动时抽屉导航未打开

android - css :focus not working in iOS Safari/Chrome, 在 Android/PC Chrome 上正常

java - 如何将项目添加到我的操作栏?

填写输入类型后,Android 键盘显示 "Go"键而不是 "next"