Android:向活动添加片段

7

我对Android还比较陌生,所以这个问题可能很显而易见,但我似乎找不到答案。

我正在编写一个计算器应用的版本。我想让用户在点击其中一个按钮时启动一个片段(片段上有更多的按钮,用户可以使用它们进行更多的输入)。

以下是片段代码:

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方法添加它,像这样:

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)的视图。”

于是我在主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>

但是这样会导致在活动创建时(并运行setContentView(R.layout.main);)立即存在该片段。

是否有一种简单的方法在活动中以编程方式添加和删除具有用户界面的片段?

2个回答

11

问题在于片段需要位于一个具有视图ID的容器内。将其设置为布局ID将导致您看到的错误。

您可以将片段添加到您的相对布局中,但为了正确地执行此操作,您需要分配适当的布局参数,以便它可以被放置。更容易的方法是在相对布局中创建一个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>
那么。
fragmentTransaction.add(R.id.FragmentContainer, fragment);

1
+1. @Spencer,如果你的片段可以作为一个独立的活动,则可以将其更改为扩展FragmentActivity并像常规活动一样处理它。 - Joe
非常感谢,那个完美地解决了问题。我不认为FragmentActivity在这种情况下适用,但我会记住这个选项以备将来使用。 - Spencer

0

在你的活动中使用简单的代码

getSupportFragmentManager().beginTransaction().add(R.id.yourcontainer,new yourfragment).commit();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接