如何以编程方式向片段添加按钮 [英] How to add a button programmatically to a fragment
问题描述
我无法以编程方式向片段添加按钮.(按钮的数量是可变的)
I can't manage to add programmatically a button to a fragment. (The number of buttons is variable)
我尝试使用一个" addView方法将按钮添加到rootView,但是没有一个.
I tried to add the button to the rootView with "an" addView method, but there isn't one.
我尝试在膨胀按钮之前将按钮添加到布局中,但是我不知道如何获取布局:(类型为id的预期资源错误)
I tried to add the button to the layout before inflating it, but I don't know how to get the layout : (Expected resource of type id error)
RelativeLayout rl = getView().findViewById(R.layout.fragment_main);
但是当充气机充气时,资源是好的.
But when the inflater inflates it, the resource is good.
我尝试将按钮添加到"onViewCreated"方法中,但是findViewById(R.layout.fragment_main)遇到了同样的问题,告诉我有一个错误(ID类型的预期资源).
I tried to add the button in the "onViewCreated" method, but I got the same problem with findViewById(R.layout.fragment_main) telling me there is an error (Expected resource of type id).
当我创建这样的按钮时:
When I create a button like this :
Button bt = new Button(this);
此"不是有效的上下文.不知道该用什么.
"this" is not a valid context. Don't know what to use instead.
那么,我该如何实现呢?
So, how can I achieve it ?
基本代码:
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
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
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上,您的代码中有几处需要纠正:
There are a few thing to correct in your code, on the fragment_main.xml:
如果要访问xml上的元素,则需要为其赋予唯一的ID.对于您的相对布局,您会丢失它,因此您将无法在片段视图中找到它.
If you want to access an element on an xml, you need to give it a unique id. In the case of your relative layout you are missing it and therefore you won't be able to find it on the 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>
现在在您的片段类中可以找到它!
Now in your fragment class you can find it!
@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;
}
就是这样,您的布局上有了一个新按钮
And that's it, you have a new button on your layout
这篇关于如何以编程方式向片段添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!