【问题标题】:ScrollView isn`t working. What should I do?滚动视图不工作。我该怎么办?
【发布时间】:2019-06-29 00:09:25
【问题描述】:

如何在我的 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>

我该怎么办?我想要这些 Cardview 在我的 Scrollview 中。

【问题讨论】:

    标签: java android scrollview


    【解决方案1】:

    将您的 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>
    

    【讨论】:

    • 好人,非常感谢您的评论...我真的以为我的应用程序已经没有希望了...但是谢谢我做到了...谢谢您真的很感激非常....
    【解决方案2】:

    使用 ScrollView 代替 RelativeLayout。在第一个 TextView 之前添加一个垂直方向的 LinearLayout,并在 CardView 之后关闭 LinearLayout。

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2019-07-04
      • 2011-06-28
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多