安卓:添加一个片段活动片段

由网友(乖一点就抱你)分享简介:我是相当新的机器人,所以这可能有一个明显的答案,但我似乎无法找到它。I'm fairly new to Android, so this might have an obvious answer, but I can't seem to find it.我正在写一个版本的计算器应用程序。我想使它使得当用户点击按钮中...

我是相当新的机器人,所以这可能有一个明显的答案,但我似乎无法找到它。

I'm fairly new to Android, so this might have an obvious answer, but I can't seem to find it.

我正在写一个版本的计算器应用程序。我想使它使得当用户点击按钮中的一个,这个启动的片段(与在其上多个按钮,它们然后可以使用更多的输入)。

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).

该片段code是如下:

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);
}

}

与XML布局如下:

with an XML layout as follows:

<?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>

(我知道我不应该使用硬codeD字符串,但我会解决,一旦我得到的东西跑了)

(I know I shouldn't be using hard-coded strings, but I'll fix that once I get the thing running)

我想有它增加了使用fragmenttransaction与激活按钮的onTouch方法,像这样:

I tried having it added using a fragmenttransaction with the onTouch method of the activating button, like so:

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:......(主)的片段VariableMenu

but that gave an error saying there was "No view found for id:...(main) for fragment VariableMenu."

于是我主要的XML文件中做了一个看法吧:

So then I made a view for it in the main XML file:

<?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);跑)。

But this resulted in the fragment being there as soon as the activity was created (and setContentView(R.layout.main); ran).

有没有一种简单的方法以编程方式添加和删除片段与用户界面的活动?

Is there a simple way to programmatically add and remove a fragment with a UI to an activity?

推荐答案

这里的问题是,该片段需要一个容器中,以期ID居住之内。给它一个布局ID将导致您看到的错误。

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.

您可以添加片段到你的亲戚布局,但要做到这一点正确,你需要给它指定适当的布局参数,以便它可以放置。这将是容易只是创建一个包装它的内容相对布局内的FrameLayout,然后添加片段存在

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.

<?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);
阅读全文

相关推荐

最新文章