简体   繁体   English

Android Textview滚动问题

[英]Android Textview Scrolling Issue

Firstly, I am new to android so if I have missed something basic I do apologise. 首先,我是android的新手,所以如果我错过了一些基本的知识,我会道歉。

I have created a GridView to store some information. 我创建了一个GridView来存储一些信息。 The GridView consists of a 3x6 grid. GridView由3x6网格组成。 In each cell, there is an image and text directly under the image. 在每个单元格中,都有一个图像和该图像正下方的文本。 As the GridView is so large I have implemented a scrollbar. 由于GridView太大,因此我实现了滚动条。

This is how the page should look normally and is when first loaded/selected : 这是页面正常外观以及首次加载/选择时的外观: 在此处输入图片说明

Everything looks wonderful, however, when I scroll down to the bottom and then back up, the text seems to wrap up and then push the image out of place. 一切看起来都很不错,但是,当我向下滚动到底部然后又向上滚动时,文本似乎会自动换行,然后将图像推到不合适的位置。 I am not sure why this is? 我不确定为什么会这样吗?

This is how the page looks after I have scrolled down and they scroll back up : 这是我向下滚动并向后滚动时页面的外观: 在此处输入图片说明

For the GridView, I have an XML file with the GridView itself along with a TextView at the very top called content_rewards.xml. 对于GridView,我有一个带有GridView本身的XML文件以及一个位于顶部的TextView,称为content_rewards.xml。 I have also create an XML file for each picture/text in the grid itself called relics_gridlayout.xml. 我还为网格本身中的每个图片/文本创建了一个XML文件,称为relics_gridlayout.xml。 I believe I am probably missing a property of the TextViews in either one of these files, however, I am not sure which one or which property. 我相信我可能在这些文件之一中都缺少TextViews的属性,但是,我不确定哪个或哪个属性。 I don't think it has anything to do with the grid or adapter itself hence I will not include that code. 我认为它与网格或适配器本身没有任何关系,因此我将不包含该代码。

content_rewards.xml content_rewards.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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">

<GridView
    android:id="@+id/customgrid"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@+id/os_texts"
    android:columnWidth="100dp"
    android:numColumns="auto_fit"
    android:stretchMode="spacingWidthUniform"
    android:verticalSpacing="35dp"
    android:scrollbars="horizontal"
    tools:layout_constraintTop_creator="1"
    android:layout_marginTop="116dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<TextView
    android:id="@+id/textView4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="B = 0   S = 0   G = 0"
    android:textSize="24sp"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    app:layout_constraintBottom_toTopOf="@+id/customgrid"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="8dp"
    app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>

relics_gridlayout.xml relics_gridlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp">

<ImageView
    android:id="@+id/os_images"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:layout_below="@+id/os_images"
    android:id="@+id/os_texts"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is Just Dummy Text"
    android:textAlignment="center"
    android:textSize="18dp"
    android:textStyle="bold"/>

</RelativeLayout>

Any help would be great. 任何帮助都会很棒。 Thanks. 谢谢。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="#ECEFF1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <GridView
        android:id="@+id/grd1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="center"
        android:horizontalSpacing="6dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="6dp">

    </GridView>


</RelativeLayout>

This should solve your problem. 这应该可以解决您的问题。

The scrolling view behavior will handle the scrolling irrespective of the size 滚动视图行为将处理滚动,而与大小无关

Now add the Text view out of the Relative layout to view it always. 现在,从“相对”布局中添加“文本”视图以始终查看它。

Or have a parent as Nested scrolling View and allow the nested scrolling enabled as false 或者将父级作为嵌套滚动视图,并允许将嵌套滚动启用为false

setNestedScrollingEnabled(false)

A sample from my code that I already have used successfully to resolve such issue 我的代码中已成功使用它解决了此问题的示例

Use your Grid view instead of Recycler View. 使用网格视图而不是“回收者视图”。

If you want the Item Text View to be always of 2 lines then set munLines="2" in the item Layout 如果希望项目文本视图始终为2行,则在项目布局中设置munLines =“ 2”

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_container_reports"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/tab_header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:text="@string/user_reports"
                    android:textAllCaps="true"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="@dimen/tab_label"
                    app:textAllCaps="true" />


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/reports_recycler"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:fadeScrollbars="false"
                        android:nestedScrollingEnabled="false"
                        android:padding="4dp" />
                </RelativeLayout>
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>

This is a problem that i faced as well , the only solution that i could come up with was hardcoding the height of the grid item. 这也是我同样面临的一个问题,我唯一能想到的解决方案是对网格项目的高度进行硬编码。

 <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:padding="3dp">

A better option would be to use recycler view with a grid layout manager. 更好的选择是将回收者视图与网格布局管理器一起使用。

you can use LinearLayout and assign weights so that every item in your grid view occupy same space 您可以使用LinearLayout并分配权重,以便网格视图中的每个项目都占据相同的空间

Update the relics_gridlayout.xml with below code ( you may need to adjust gravity and layout weights)Try this 使用以下代码更新relics_gridlayout.xml(您可能需要调整重力和布局权重)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/linearLayout_root_single_home_option"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">


<ImageView
    android:id="@+id/os_images"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_weight="9" />


<TextView
    android:id="@+id/os_texts"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is Just Dummy Text"
    android:textSize="18dp"
    android:textStyle="bold"
    android:layout_weight="1" />


</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM