Android ScrollView example

Android ScrollView example

ScrollViewIt is definitely a very important view that various teaching materials and tutorials have more or less missed. If the composition of this interface is very irregular, and the length in the correct direction is not enough, it must be used Scrollview. Because it ListViewdeals with the content of rules. As for a simple example, I will discover how to use Scrollview.

There are a few news pages with pictures. Under the pictures, you can click the button to learn more, there are titles, and finally all the news content. If the content is warriors, the list view does not seem to be the best choice, but generally The layout, for example LinearLayout, RelativeLayoutor FrameLayoutthe like layout is also useless. In the end, only the ScrollViewproblem can be solved.

ScrollViewIt is such a special layout. When ScrollViewthe content is larger than its own size, it ScrollViewwill automatically add a scroll bar and slide vertically.

  1. ScrollViewThere can only be one direct child view of. If you want to use a very complicated view structure, like the news mentioned in the previous question, you must put these views in a standard layout, such as LinearLayout, RelativeLayoutetc.
  2. You can use layout_widthand layout_heightgive the ScrollViewspecified size.
  3. ScrollViewOnly batch process combinations of irregular views that require scrolling. Large-scale list data display can be used ListView, GridViewor RecyclerView.
  4. ScrollViewAnd ListViewthere will be a conflict nested slide such use. Don't use it unless you have to.
  5. ScrollViewOnly supports vertical sliding, horizontal sliding use HorizontalScrollView.
  6. ScrollViewThe android:fillViewportattribute of defines whether the content can be stretched to fill the viewport. You can call methods setFillViewport(boolean)to achieve the same effect.

Android ScrollView example

Insert picture description here

demo.png

layout

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


Continuously updating...
Article reference learning address: https://www.jianshu.com/p/ddd295a06d17

Guess you like

Origin blog.csdn.net/weixin_44325444/article/details/107185308