java - TextView 阻止底部导航

标签 java android xml

我在开发的 Android 应用程序中遇到一个问题,如果文本太长,包含 TextView 的 fragment 中的文本会覆盖底部导航。该问题如下所示:

TextView 阻止底部导航

enter image description here

这是 fragment 的代码:

package com.example.xxxxxx.loremipsum;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link LessonFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link LessonFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LessonFragment extends Fragment
{
    private String lessonContent;
    private OnFragmentInteractionListener mListener;
    TextView lesson;
    private int lessonID;
    private boolean culture;
    public LessonFragment()
    {

    }

    @SuppressLint("ValidFragment")
    public LessonFragment(int lessonID)
    {
        this.lessonID = lessonID;
        culture = false;
    }

    @SuppressLint("ValidFragment")
    public LessonFragment(int lessonID, int i)
    {
        this.lessonID = lessonID;
        culture = true;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment LessonFragment.
     */
    public static LessonFragment newInstance(String param1, String param2)
    {
      return null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
        {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_lesson, container, false);
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        Button button = (Button) view.findViewById(R.id.backButton);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if(!culture)
                {
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    GrammarFragment GF = new GrammarFragment();
                    fragmentTransaction.replace(R.id.container, GF);
                    fragmentTransaction.commit();
                }
                else
                {
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    CultureFragment CF = new CultureFragment();
                    fragmentTransaction.replace(R.id.container, CF);
                    fragmentTransaction.commit();
                }
            }
        });
        lesson = (TextView) view.findViewById(R.id.grammarContent);
        String[] lessons;
        if(!culture)
             lessons = getActivity().getResources().getStringArray(R.array.lessons);
        else
             lessons = getActivity().getResources().getStringArray(R.array.cultureLessons);
        lessonContent = lessons[lessonID];
        lesson.setText(lessonContent);
        return view;
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener)
        {
            mListener = (OnFragmentInteractionListener) context;
        }
        else
        {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }


    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener
    {
        void onBackClickCulture();
    }


}

这是 fragment 的 XML:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.example.xxxxxx.loremipsum.LessonFragment">

<ScrollView
    android:layout_width="wrap_content"
    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:id="@+id/grammarContent"
            android:textSize="18sp"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Back"
            android:id="@+id/backButton"
            tools:ignore="OnClick" />
    </LinearLayout>
</ScrollView>

最后,这是我的 MainActivity 的 XML,其中添加了 Fragment:

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xxxxxx.loremipsum.MainActivity">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

编辑:这是另一个问题的屏幕截图:

这是我遇到的错误

enter image description here

最佳答案

请在您的 MainActivity xml 部分中再添加一个布局。并且您还可以将 MainActivity R.id.fl_main 更改为 R.id.container

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.buttonnavifragment.MainActivity">

    <FrameLayout
        android:id="@+id/fl_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

关于java - TextView 阻止底部导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51868428/

相关文章:

java - 在 setter 中使用 switch 语句

android - 如何定位在 ConstraintLayout 中两个 View 中最低的一个下方?

java - 使用序列化是个好主意吗?

java - PHP 和 Java Unix 时间戳之间的差异

java - 首先发生什么: onCreate or configureFlutterEngine?

java - Split() 函数用于字符串末尾的两个连续正则表达式

安卓Wifi流量

android - 获取 LiveData 对象的更有效方法

Python - 循环的深度 XML 文件

javascript - 使用 xpath 计算具有特定属性值的节点?