Android basic controls (3)

Keywords: Android iOS

10.ScrollView

When the content to be displayed on the interface is larger than one screen, we will use scroll view or subclass of scroll view.
ScrollView can only scroll up and down.
However, HorizontalScrollView can only slide left and right.

However, there can only be one view in the scrolling view, so it is common to place multiple views in the layout.
Example code:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!"
                />

        </LinearLayout>

    </ScrollView>

The use of HorizontalScrollView is similar to that of ScrollView.

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="800dp"
                android:layout_height="wrap_content"
                android:textSize="40sp"
                android:text="Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!"
            />
        </LinearLayout>

    </HorizontalScrollView>

11.WebView

<WebView
        android:id="@+id/content_webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</WebView>


WebView webView = (WebView)findViewById(R.id.content_webView);
        webView.loadUrl("http://www.sina.com");

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        
        webView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // Override this method to prevent the system browser from opening automatically when the web page is loaded
                view.loadUrl(url);

                return true;
            }
        });

12.ListView

ListView is equivalent to UITableView in iOS, which is mainly used to display views with basically the same content.

    <ListView
        android:id="@+id/content_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
    
     private String[] data = { "Apple", "A mandarin orange", "Banana", "Peach", "Pear", "Kiwifruit", "watermelon", "Grape", "pineapple", "Mango", "Red dates", "Carambola", "Cherry", "Hami melon", "longan", "Litchi"};
     
     ListView listView = (ListView)findViewById(R.id.content_list_view);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, data);
        listView.setAdapter(adapter);

Of course, if the Cell style we want is complex, we need to customize the Adapter and item.

Posted by mw-dnb on Thu, 19 Dec 2019 13:49:52 -0800