Android的ScrollView示例

Android的ScrollView示例

ScrollView绝对是各种教材,教程都或多或少遗漏的一个非常重要的视图。凡是这个界面的组成非常不规则,而且正确方向长度不够就肯定需要使用Scrollview了。因为ListView处理的是规则的内容。至于会通过一个简单的例子,发现如何使用Scrollview。

少数一则新闻页,有配图,在配图下可以点击按钮了解更多,有标题,最后是全部的新闻内容,假设这则内容是勇士然后列表视图似乎不是最好的选择,但是一般的布局,例如LinearLayoutRelativeLayout或者FrameLayout之类布局也没法用。最后就只能ScrollView可以解决问题了。

ScrollView就是这么一种特殊的布局。当ScrollView的内容大于他本身的大小的时候,ScrollView会自动添加滚动条,并可以垂直滑动。

  1. ScrollView的直接子视图只能有一个。这就如果你要使用很复杂的视图结构,就如上问中说的那条新闻,你必须把这些视图放在一个标准布局里,如LinearLayoutRelativeLayout等。
  2. 你可以使用layout_widthlayout_heightScrollView指定大小。
  3. ScrollView仅批量处理需要滚动的不规则视图的组合。大批量的列表数据展示可以使用ListViewGridView或者RecyclerView
  4. ScrollViewListView之类的嵌套使用时会有滑动冲突。不到不得已不要使用。
  5. ScrollView只支持垂直滑动,水平滑动使用HorizontalScrollView
  6. ScrollViewandroid:fillViewport属性定义了是否可以拉伸其内容来填满视口。您可以可以调用方法setFillViewport(boolean)来达到一样的效果。

Android的ScrollView示例

在这里插入图片描述

demo.png

布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:padding="10dp"
    tools:context="demo.scrollviewdemo.MainActivity">

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

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/nba" />

        <Button
            android:id="@+id/knowMoreButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/know_more" />

        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/contentTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/content"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>
</ScrollView>


持续更新中…
文章参考学习地址:https://www.jianshu.com/p/ddd295a06d17

猜你喜欢

转载自blog.csdn.net/weixin_44325444/article/details/107185308