Android:向活动添加片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10419924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Adding a fragment to an activity
提问by Spencer
I'm fairly new to Android, so this might have an obvious answer, but I can't seem to find it.
我对 Android 相当陌生,所以这可能有一个明显的答案,但我似乎找不到它。
I'm writing a version of a calculator app. I want to make it so that when a user clicks on one of the buttons, this starts a fragment (with more buttons on it that they can then use for more input).
我正在编写一个计算器应用程序的版本。我想这样做,当用户单击其中一个按钮时,这会启动一个片段(上面有更多按钮,然后他们可以用于更多输入)。
The fragment code is as follows:
片段代码如下:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class VariableMenu extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment, container, false);
}
}
with an XML layout as follows:
具有如下 XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/Bx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X"
android:onClick="onButtonClicked"/>
<Button
android:id="@+id/By"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/Bx"
android:layout_alignTop="@id/Bx"
android:text="Y"
android:onClick="onButtonClicked"/>
<Button
android:id="@+id/Bz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/By"
android:layout_alignTop="@id/By"
android:text="Z"
android:onClick="onButtonClicked"/>
</RelativeLayout>
(I know I shouldn't be using hard-coded strings, but I'll fix that once I get the thing running)
(我知道我不应该使用硬编码的字符串,但是一旦我开始运行,我就会解决这个问题)
I tried having it added using a fragmenttransaction with the onTouch method of the activating button, like so:
我尝试使用 fragmenttransaction 和激活按钮的 onTouch 方法添加它,如下所示:
public void onFragmentActivated(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
VariableMenu fragment = new VariableMenu();
fragmentTransaction.add(R.layout.main, fragment);
fragmentTransaction.commit();
}
but that gave an error saying there was "No view found for id:...(main) for fragment VariableMenu."
但这给出了一个错误,指出“没有找到 id:...(main) for fragment VariableMenu 的视图”。
So then I made a view for it in the main XML file:
然后我在主 XML 文件中为它创建了一个视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
(Irrelevant things)
<fragment
android:id="@+id/Fragment"
android:name="calculator.app.VariableMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/B1"/>
(Irrelevant things)
</RelativeLayout>
But this resulted in the fragment being there as soon as the activity was created (and setContentView(R.layout.main); ran).
但这导致片段在创建活动后立即出现(并且 setContentView(R.layout.main); 运行)。
Is there a simple way to programmatically add and remove a fragment with a UI to an activity?
是否有一种简单的方法可以以编程方式向活动添加和删除带有 UI 的片段?
回答by dagalpin
The problem here is that the fragment needs a container with a view id to reside within. Giving it a layout id will result in the error you saw.
这里的问题是片段需要一个带有视图 ID 的容器来驻留。给它一个布局 id 会导致你看到的错误。
You can add the fragment to your relative layout, but to do that properly you'll need to assign it appropriate layout parameters so that it can be placed. It would be easier to just create a FrameLayout within the relative layout that wraps its contents, and then add the fragment there.
您可以将片段添加到您的相对布局,但要正确执行此操作,您需要为其分配适当的布局参数,以便可以放置它。在包装其内容的相对布局中创建一个 FrameLayout 会更容易,然后在那里添加片段。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/FragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/B1"/>
</RelativeLayout>
Then
然后
fragmentTransaction.add(R.id.FragmentContainer, fragment);
回答by Spencer
use easy code in your activity
在您的活动中使用简单的代码
getSupportFragmentManager().beginTransaction().add(R.id.yourcontainer,new yourfragment).commit();