无法滚动我的TextView

时间:2018-04-19 15:37:19

标签: android android-layout android-studio android-fragments scroll

我读了一些方法,如何使TextView可滚动。 这是我片段中的一些代码:

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

        // 1. pass context and data to the custom adapter
        adapter = new AdapterHandlungsleitfaden(getContext(), generateData());

        // if extending Activity 2. Get ListView from activity_main.xml
        final ListView listView = (ListView) view.findViewById(R.id.listview);

        // 3. setListAdapter
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                models.add(new ListItemDataHandlungsleitfaden(R.drawable.uncheck,"" + counter + "." + value, "" + counter));
                counter++;

                Toast.makeText(getContext(), "ITEM WIRD ERWEITERT!", Toast.LENGTH_SHORT).show();
                adapter.notifyDataSetChanged();
            }
        });

        //initialize TextView - Title
        TextView title = (TextView) view.findViewById(R.id.title_handlungsleitfaden);
        title.setText("Das ist nur ein Test!");

        //initialize TextView - Content
        TextView textView = (TextView) view.findViewById(R.id.text_handlungsleitfaden);
        textView.setText("HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!" +
                "HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!" +
                "HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!HALLO WELT!" +
                "DU NE HAHA GENIAL!");

        textView.setMovementMethod(new ScrollingMovementMethod());

        return view;
    } 

如您所见,我使用了这种方法:

textView.setMovementMethod(new ScrollingMovementMethod()); 

就这样,我试图让我的TextView可滚动。

这是片段的xml-layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".uiFragments.HandlungsleitfadenFragment">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ListView
            android:id="@+id/listview"
            android:layout_height="wrap_content"
            android:layout_width="250dp"/>

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:layout_marginLeft="260dp"
            android:layout_marginBottom="50dp">

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

                <TextView
                    android:id="@+id/title_handlungsleitfaden"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:textStyle="bold"
                    android:typeface="sans" />

                <TextView
                    android:id="@+id/text_handlungsleitfaden"
                    android:layout_width="wrap_content"
                    android:layout_height="200dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/textview_border" />

                <ImageView
                    android:id="@+id/bild_handlungsleitfaden"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/homer"/>

                <FrameLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <VideoView
                        android:id="@+id/video_handlungsleitfaden"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                </FrameLayout>

            </LinearLayout>

        </ScrollView>

        <Button
            android:id="@+id/handlungsleitfaden_btn"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="7dp"
            android:textAllCaps="false"
            android:text="weiter" />

    </RelativeLayout>

</FrameLayout>

我可以滚动显示,但我不能在TextView中滚动。因为,当我尝试这样做时,我只能滚动我的“整个”显示“像文本框,标题,图像和视频。但我想要一种方法,也可以滚动这个显示,就像我现在可以做的那样,以及TextView内的ScrollView

如果有人可以帮助我,我会很开心!

感谢您的建议!

1 个答案:

答案 0 :(得分:1)

只需在要xml TextView的{​​{1}}文件中设置这些属性即可。

android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"

AN_INTEGER的地方你可以输入任何整数,如:1,2,3 ,,,, 100等。

然后,在您的Activity课程中使用此

textView.setMovementMethod(new ScrollingMovementMethod());