我是 Android 的新手,所以这可能有一个明显的答案,但我似乎找不到。
我正在编写一个计算器应用程序版本。我想这样做,以便当用户单击其中一个按钮时,这将启动一个 fragment (上面有更多按钮,然后他们可以使用这些按钮进行更多输入)。
fragment 代码如下:
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);
}
}
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>
(我知道我不应该使用硬编码字符串,但一旦我开始运行我会修复它)
我尝试使用带有激活按钮的 onTouch 方法的 fragmenttransaction 添加它,如下所示:
public void onFragmentActivated(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
VariableMenu fragment = new VariableMenu();
fragmentTransaction.add(R.layout.main, fragment);
fragmentTransaction.commit();
}
但这给出了一个错误,指出“没有找到 id:...(main) fragment VariableMenu 的 View 。”
然后我在主 XML 文件中为其创建了一个 View :
<?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>
但这导致 fragment 在 Activity 创建后立即出现(并且 setContentView(R.layout.main); 运行)。
是否有一种简单的方法可以通过编程方式向 Activity 添加和删除带有 UI 的 fragment ?
最佳答案
这里的问题是 fragment 需要一个带有 View ID 的容器驻留在其中。给它一个布局 ID 将导致您看到的错误。
您可以将 fragment 添加到您的相关布局中,但要正确执行此操作,您需要为其分配适当的布局参数以便放置它。在包装其内容的相对布局中创建一个 FrameLayout,然后在其中添加 fragment 会更容易。
<?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>
然后
fragmentTransaction.add(R.id.FragmentContainer, fragment);
关于Android:将 fragment 添加到 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10419924/