Android Textview滚动问题

时间:2017-07-05 10:43:36

标签: android xml gridview

首先,我是android的新手,所以如果我错过了一些基本的东西我会道歉。

我创建了一个GridView来存储一些信息。 GridView由3x6网格组成。在每个单元格中,图像和文本直接位于图像下方。由于GridView太大,我实现了一个滚动条。

这是页面正常显示的方式,也就是首次加载/选择时: enter image description here

但是,当我向下滚动到底部然后再向上时,一切看起来都很美妙,文本似乎结束然后将图像推出原位。我不知道为什么会这样?

这是我滚动向下滚动后页面的显示方式: enter image description here

对于GridView,我有一个带有GridView本身的XML文件,以及最顶层的一个名为content_rewards.xml的TextView。我还为网格中的每个图片/文本创建了一个名为relics_gridlayout.xml的XML文件。我相信我可能在其中一个文件中缺少TextViews的属性,但是,我不确定哪一个或哪个属性。我不认为它与网格或适配器本身有任何关系,因此我不会包含该代码。

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

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

任何帮助都会很棒。感谢。

3 个答案:

答案 0 :(得分:0)

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

这可以解决您的问题。

滚动视图行为将处理滚动而不管大小

现在从“相对”布局中添加“文本”视图以始终查看它。

或者让父作为嵌套滚动视图并允许嵌套滚动启用为假

setNestedScrollingEnabled(false)

我已成功用于解决此类问题的代码示例

使用网格视图而不是Recycler View。

如果您希望项目文本视图始终为2行,则设置munLines =&#34; 2&#34;在项目布局

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

答案 1 :(得分:0)

这也是我遇到的一个问题,我能想出的唯一解决方案是硬编码网格项的高度。

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

更好的选择是使用带有网格布局管理器的回收站视图。

答案 2 :(得分:0)

您可以使用LinearLayout并指定权重,以便网格视图中的每个项目占用相同的空间

使用以下代码更新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>
相关问题