gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:05:14 25 4
gpt4 key购买 nike

我无法设法以编程方式向 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com