Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文本视图阻止底部导航_Java_Android_Xml - Fatal编程技术网

Java 文本视图阻止底部导航

Java 文本视图阻止底部导航,java,android,xml,Java,Android,Xml,我正在开发的Android应用程序中有一个问题,如果文本太长,则包含TextView的片段中的文本会覆盖底部导航。问题是这样的: TextView阻止底部导航 下面是片段的代码: package com.example.xxxxxx.loremipsum; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; import an

我正在开发的Android应用程序中有一个问题,如果文本太长,则包含
TextView
的片段中的文本会覆盖底部导航。问题是这样的:

TextView
阻止底部导航

下面是片段的代码:

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();
    }


}
<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>
package com.example.xxxxxx.loremipsum;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.content.res.Resources;
导入android.net.Uri;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.constraint.ConstraintLayout;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentTransaction;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
/**
*一个简单的{@link Fragment}子类。
*包含此片段的活动必须实现
*{@link LessonFragment.OnFragmentInteractionListener}接口
*处理交互事件。
*使用{@link LessonFragment#newInstance}工厂方法
*创建此片段的实例。
*/
公共类LessonFragment扩展了片段
{
私密的弦乐内容;
私有OnFragmentInteractionListener mListener;
文本视图课程;
莱索尼德私人酒店;
私有布尔文化;
公共课
{
}
@SuppressLint(“有效碎片”)
公共莱索尼德(内莱索尼德)
{
this.lessonID=lessonID;
文化=假;
}
@SuppressLint(“有效碎片”)
公共LessonFragment(内莱索尼德,内莱索尼德)
{
this.lessonID=lessonID;
文化=真实;
}
/**
*使用此工厂方法创建的新实例
*使用提供的参数创建此片段。
*
*@param param1参数1。
*@param param2参数2。
*@return一个fragment LessonFragment的新实例。
*/
公共静态LessonFragment newInstance(字符串param1,字符串param2)
{
返回null;
}
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
如果(getArguments()!=null)
{
}
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState)
{
视图=充气机。充气(R.layout.fragment\u课程,容器,错误);
ConstraintLayout.LayoutParams LayoutParams=新的ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.WRAP_内容);
view.setLayoutParams(layoutParams);
Button Button=(Button)view.findViewById(R.id.backButton);
setOnClickListener(新视图.OnClickListener()
{
@凌驾
公共void onClick(视图)
{
如果(!文化)
{
FragmentManager FragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction();
GrammarFragment GF=新的GrammarFragment();
碎片事务。替换(R.id.container,GF);
fragmentTransaction.commit();
}
其他的
{
FragmentManager FragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction();
CultureFragment CF=新的CultureFragment();
fragmentTransaction.replace(R.id.container,CF);
fragmentTransaction.commit();
}
}
});
lesson=(TextView)view.findViewById(R.id.grammarContent);
字符串[]课程;
如果(!文化)
课程=getActivity().getResources().getStringArray(R.array.lessons);
其他的
课程=getActivity().getResources().getStringArray(R.array.CultureResons);
lessonContent=课程[莱索尼德];
lesson.setText(第二课内容);
返回视图;
}
@凌驾
公共void-onAttach(上下文)
{
super.onAttach(上下文);
if(OnFragmentInteractionListener的上下文实例)
{
mListener=(OnFragmentInteractionListener)上下文;
}
其他的
{
抛出新的RuntimeException(context.toString()
+“必须实现OnFragmentInteractionListener”);
}
}
@凌驾
公共无效连接()
{
super.onDetach();
mListener=null;
}
/**
*此接口必须由包含以下内容的活动实现
*片段,以允许通信此片段中的交互
*该活动以及其中可能包含的其他片段
*活动。
*
*有关更多信息,请参阅Android培训课程。
*/
FragmentInteractionListener上的公共接口
{
在BackClickCulture()上无效;
}
}
以下是片段的XML:

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();
    }


}
<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>

最后,这里是my MainActivity的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: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>


请在
MainActivity
xml部分再添加一个布局。您还可以使用
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>


  • 您的容器应该是一个视图,该视图应该位于
    BottomNavigationView
    之上。这就是为什么你会有这些冲突。因为您只需在父视图上附加片段
  • 始终将背景设置为
    Fragmen