android - 如何使 TextView 在 fragment 中可滚动?

标签 android android-fragments textview android-scroll

我在 Activity 中创建了 fragment 。每个 fragment 都有一个 TextView 。我想让 fragment 内的所有 TextView 都可以滚动。下面是我的 xml 和 java 代码。提前致谢

<FrameLayout 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=".chemistry.AtomicNumber">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/atomicNumberDefinition"
            android:text="The atomic number..... "
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="24sp"
            android:scrollbars="vertical" />

Java 代码:

public class AtomicNumber extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public AtomicNumber() {
    }

    public static AtomicNumber newInstance(String param1, String param2) {
        AtomicNumber fragment = new AtomicNumber();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_atomic_number, container, false);
    }
}

请告诉我如何在java文件中编写正确的代码。

最佳答案

我建议使用ScrollView:

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/atomicNumberDefinition"
        android:text="The atomic number..... "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="24sp" />
</ScrollView>

关于android - 如何使 TextView 在 fragment 中可滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70332007/

相关文章:

android - 禁用标签之间的滑动

android - TextView 链接文字不可见

android - 可以指定 TextView 文本以缩放以适合 TextView 吗?

安卓 : Why onClick method is not working?

android - Fragment 中的 ListView 未显示在 MainActivity 中

java - Android 应用程序从 XML 打开 InputStream 时崩溃

android - 未经检查的对 ArrayAdapter super 的调用

Android 导航架构组件 - 获取当前可见 fragment

android - 如何在 libgdx 中围绕其中心拖动时旋转一个圆圈?

android - 设置应用程序 :layout_behavior programmatically