ScrollView не работает. Что я должен делать? - PullRequest
2 голосов
/ 29 июня 2019

Как прокрутить приложение для Android?

Я уже пробовал Scrollview, но он не работает.

Так что это мой MainActivity:

package com.example.myapplication2;

import android.content.DialogInterface;
import android.content.Intent;
import android.icu.text.IDNA;
import android.nfc.Tag;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import static com.example.myapplication2.R.id.*;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private boolean loadFragment(Fragment fragment){
        if(fragment!=null){
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(fragment_container, fragment)
                    .commit();
            return true;
        }
        return false;
    }

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

        BottomNavigationView navigation = findViewById(R.id.bottomnav);
        navigation.setOnNavigationItemSelectedListener(this);

        getSupportFragmentManager().beginTransaction().replace(fragment_container, new Home_Screen()).commit();
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Fragment fragment = null;

        switch(menuItem.getItemId()){
            case kontakt:
                break;

            case Termine:
                fragment = new TermineFragment();
                break;

            case wilkommen:
                fragment = new Home_Screen();
                break;              
        }

        return loadFragment(fragment);
    }
}

А это мой XML-файл:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:background="@color/darkgrey">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Termine"
            android:textSize="30sp"
            android:textColor="@color/colorAccent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            ></TextView>

    <android.support.v7.widget.CardView

            android:layout_width="match_parent"
            android:layout_height="173dp"
            app:cardCornerRadius="8dp"
            android:layout_marginTop="80dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            >

        <Button
                android:text="Mehr infos"
                android:layout_width="120dp"
                android:layout_height="35dp"
                android:id="@+id/button1"
                android:layout_weight="1"
                android:layout_marginTop="130dp"
                android:layout_marginRight="170dp"
                />

        <LinearLayout android:layout_height="match_parent"
                      android:layout_width="match_parent"
                      android:background="@color/maincolor"

                >
        </LinearLayout>

        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="HPI - 29.06.19"
                  android:textSize="24dp"
                />

        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="Sitzung: H-E.51"
                  android:textSize="16dp"
                  android:layout_marginTop="40dp"

                />
        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="Juni22, 2019"
                  android:textSize="16dp"
                  android:layout_marginTop="40dp"
                  android:layout_marginRight="20dp"
                  android:layout_marginLeft="270dp"
                />
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView

            android:layout_width="match_parent"
            android:layout_height="173dp"
            app:cardCornerRadius="8dp"
            android:layout_marginTop="270dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            >

        <LinearLayout android:layout_height="match_parent"
                      android:layout_width="match_parent"
                      android:background="@color/colorPrimaryDark"

                />

        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="HPI - 13.07.19"
                  android:textSize="24dp"
                />

        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="Sitzung: H-E.51"
                  android:textSize="16dp"
                  android:layout_marginTop="40dp"

                />
        <TextView android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:textColor="@color/white"
                  android:text="July13, 2019"
                  android:textSize="16dp"
                  android:layout_marginTop="40dp"
                  android:layout_marginRight="20dp"
                  android:layout_marginLeft="270dp"

                />
    </android.support.v7.widget.CardView>
</RelativeLayout>

Что мне делать? Я хочу эти Cardviews в моем Scrollview.

Ответы [ 2 ]

2 голосов
/ 29 июня 2019

Замените ваш код XML-макета на следующий. Также, будьте уведомлены, кажется, что между вашими двумя картами есть неловкое количество topMargin, не уверенный, предназначено ли это или нет, но на данный момент между вашими двумя картами есть огромный разрыв:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/darkgrey">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Termine"
            android:textSize="30sp"
            android:textColor="@color/colorAccent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

        <android.support.v7.widget.CardView

            android:layout_width="match_parent"
            android:layout_height="173dp"
            app:cardCornerRadius="8dp"
            android:layout_marginTop="80dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            >

            <Button
                android:text="Mehr infos"
                android:layout_width="120dp"
                android:layout_height="35dp"
                android:id="@+id/button1"
                android:layout_weight="1"
                android:layout_marginTop="130dp"
                android:layout_marginRight="170dp" />


            <LinearLayout android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:background="@color/maincolor">

            </LinearLayout>

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="@color/white"
                android:text="HPI - 29.06.19"
                android:textSize="24dp" />

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="@color/white"
                android:text="Sitzung: H-E.51"
                android:textSize="16dp"
                android:layout_marginTop="40dp"/>

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="@color/white"
                android:text="Juni22, 2019"
                android:textSize="16dp"
                android:layout_marginTop="40dp"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="270dp" />

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView

            android:layout_width="match_parent"
            android:layout_height="173dp"
            app:cardCornerRadius="8dp"
            android:layout_marginTop="270dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            >

            <LinearLayout android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:background="@color/colorPrimaryDark"

                />

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="@color/white"
                android:text="HPI - 13.07.19"
                android:textSize="24dp"
                />

            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="@color/white"
                android:text="Sitzung: H-E.51"
                android:textSize="16dp"
                android:layout_marginTop="40dp"

                />
            <TextView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textColor="@color/white"
                android:text="July13, 2019"
                android:textSize="16dp"
                android:layout_marginTop="40dp"
                android:layout_marginRight="20dp"
                android:layout_marginLeft="270dp"

                />

        </android.support.v7.widget.CardView>

    </LinearLayout>

</ScrollView>

</RelativeLayout>
0 голосов
/ 29 июня 2019

Используйте ScrollView вместо RelativeLayout.Добавьте LinearLayout с вертикальной ориентацией перед первым TextView и закройте LinearLayout после CardView.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...