1

我正在尝试在单个 Activity 中添加两个 AndroidFragmentApplication。但只有最后添加的片段才会真正呈现在屏幕上。其他片段不显示任何内容。我需要做什么才能使两个视图都绘制出来?

我首先尝试使用 AndroidApplication 类初始化视图。但这导致应用程序崩溃。然后,我添加了多个扩展 AndroidFragmentApplication 的片段。现在,应用程序不会崩溃,但只会在屏幕上绘制最后添加的片段。

这是我添加片段的地方-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <fragment
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:id="@+id/alpha_fragment"
        android:name="com.sample.libgdx_final.AlphaFragment" />

    <fragment
        android:id="@+id/coin_fragment"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:name="com.sample.libgdx_final.CoinFragment"
        />
</FrameLayout>

这是我的 Fragment 和 ApplicationListener -

public class CoinFragment extends AndroidFragmentApplication implements AndroidFragmentApplication.Callbacks{
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return initializeForView(new CoinTranslateLibgdx());
    }
}

CoinTranslateLibgdx.java

class CoinTranslateLibgdx extends ApplicationAdapter {
    private SpriteBatch batch;
    private Texture texture;
    private Sprite sprite;
    private float deltaTime = 0f;
    private boolean increasing = true;

    @Override
    public void create() {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("token.png"));
        sprite = new Sprite(texture);
        sprite.setCenterX(Gdx.graphics.getWidth() / 2f);
        sprite.setCenterY(Gdx.graphics.getHeight());
    }
    @Override
    public void render() {
        Gdx.gl20.glClearColor(0, 0, 0, 0);
        batch.begin();
        if (deltaTime <= 1 && increasing) {
            deltaTime += Gdx.graphics.getDeltaTime() * 1f;
        } else if (deltaTime >= 0.09) {
            increasing = false;
            deltaTime -= Gdx.graphics.getDeltaTime() * 1f;
        } else {
            increasing = true;
        }
        batch.setColor(1, 1, 1, deltaTime);
        batch.draw(sprite, 0, 0);
        batch.end();
    }
    @Override
    public void dispose() {
        batch.dispose();
        texture.dispose();
    }
}
4

0 回答 0