启动应用

时间:2018-04-10 15:51:14

标签: android xml android-fragments

我很擅长使用片段,而且一般来说都是android。我尝试加载一个片段,它似乎工作,没有任何崩溃。但是,当我查看应用程序时,它没有显示片段布局。我看过其他类似的问题和答案,但找不到我做错的事。

主要活动xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="groep10.chef.Main"
    android:background="@color/colorPrimary">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorAccent"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar">
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

将片段加载到FrameLayout的主类:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;

import groep10.chef.fragments.HomeFragment;

public class Main extends FragmentActivity {

    private FragmentManager fm;
    private Fragment mainFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fm = getSupportFragmentManager();
        mainFragment = fm.findFragmentById(R.id.fragment_container);

        if (mainFragment == null) {
            mainFragment = new HomeFragment();
            fm.beginTransaction()
                    .add(R.id.fragment_container, mainFragment)
                    .commit();
        }

    }
}

HomeFragment,我自己的扩展片段,其中加载了此片段的正确布局:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import groep10.chef.R;

public class HomeFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstancestate) {
        super.onCreate(savedInstancestate);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parentVG,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.home_screen, parentVG, false);
        System.out.println("Testjeeuh");
        return v;
    }
}

我认为没有必要,但Home Fragment的布局xml:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/recipeButtonExpl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:text="@string/recipeButtonExpl"
                android:textAlignment="center"
                android:textColor="#EBEBEB"
                app:layout_constraintBottom_toTopOf="@+id/recipeButtonText"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="1.0" />

            <Button
                android:id="@+id/recipeButtonText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/my_button"
                android:paddingBottom="40dp"
                android:paddingTop="40dp"
                android:text="@string/recipeButtonText"
                android:textColor="#EBEBEBEB"
                app:layout_constraintBottom_toTopOf="@+id/recipeButton2Text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/recipeButton2Expl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:width="200dp"
                android:text="@string/recipeButtonTwoExpl"
                android:textAlignment="center"
                android:textColor="#EBEBEB"
                app:layout_constraintBottom_toTopOf="@+id/recipeButton2Text"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="1.0" />

            <Button
                android:id="@+id/recipeButton2Text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/my_button"
                android:paddingBottom="40dp"
                android:paddingTop="40dp"
                android:text="@string/recipeButtonTwoText"
                android:textColor="#EBEBEB"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/shoppingButtonExpl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/shoppingButtonExpl"
                android:textAlignment="center"
                android:textColor="#EBEBEB"
                app:layout_constraintBottom_toTopOf="@+id/shoppingButtonText"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="1.0" />

            <Button
                android:id="@+id/shoppingButtonText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/my_button"
                android:paddingBottom="40dp"
                android:paddingTop="40dp"
                android:text="@string/shoppingButtonText"
                android:textColor="#EBEBEB"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/recipeButton2Text" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

我认为这可能是提交不在Main类中工作的问题,但我不知道为什么它不起作用。

2 个答案:

答案 0 :(得分:0)

片段布局中的ScrollView高度和宽度为0dp。片段加载在FrameLayout中,而不是替换Framelayout

试试这个ScrollView

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

答案 1 :(得分:0)

片段布局中的

。您使用0dp设置ScrollView的宽度和高度,因此片段的内容不会出现。 请尝试此代码

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
.
.
.
.
</ScrollView>