This essay will explain the more commonly used two layout containers in Android--scrollview and Horizontalscrollview, which is also very simple in the literal sense, ScrollView is a view that can be scrolled, The direction of the scroll is vertical, while horizontalscrollview is a horizontal view that can be scrolled. This essay may have less descriptive knowledge, mostly through code to see how to use these two view.
A brief introduction of ScrollView
First look at the definitions of the two view ScrollView and Horizontalscrollview. ScrollView and Horizontalscrollview are both a layout container that can be put into the child view control, which we see through its inheritance relationship, These two classes of ScrollView and Horizontalscrollview are an indirect subclass of ViewGroup.
Java.lang.Object
↳android.view.view
↳android.view.viewgroup
↳android.widget.framelayout
↳android.widget.scrollview
Java.lang.Object
↳android.view.view
↳android.view.viewgroup
↳android.widget.framelayout
↳android.widget.horizontalscrollview
Because ScrollView and Horizontalscrollview are just two kinds of scrolling direction different view, other aspects are basically the same, so the following only to ScrollView to explain.
By using ScrollView, we can scroll through the child view controls inside of it, allowing us to control the height of our controls more than the size of our actual screen. ScrollView is a framelayout, as for what is framelayout, in simple terms, framelayout is usually designed to occupy a place on the screen and there is only one item in it, we often use such as DatePicker, Timepicker These controls are part of the framelayout layout. So in ScrollView, it usually contains only one child element, and this child element is also a layout file, so that we can add any child controls we want in the layout file to achieve scrolling effect.
For ScrollView, because it is a scrolling layout in the vertical direction, we usually add a linearlayout child element to it and set the orientation to vertical (vertical). Let's take a little example to see how we can use our ScrollView to show multiple images, and to scroll the vertical direction of the picture.
First we define a scrollview, because ScrollView is also a viewgroup, so we can use ScrollView directly as the root element of our XML file:
<?xml version= "1.0" encoding= "Utf-8"?> <scrollview xmlns:android=
"Http://schemas.android.com/apk/res" /android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: Fillviewport= "false" >
<linearlayout
android:id= "@+id/layout"
android:layout_height= "Match_" Parent "
android:layout_width=" wrap_content "
android:orientation=" vertical "/>
</scrollview >
We see that under the ScrollView element we also define a linearlayout and set a linear layout whose direction is perpendicular. The action we add to the picture is done in the code. Here's a look at the Scrollviewactivity class:
public class Scrollviewactivity extends activity
{
private linearlayout layout;
public void OnCreate (Bundle savedinstancestate)
{
super.oncreate (savedinstancestate);
Setcontentview (R.layout.layout_scrollview);
Layout = (linearlayout) Findviewbyid (r.id.layout);
for (int i = 0; i < 8 i++)
{
//To obtain the specified Drawable object
drawable drawable = getresources () through a resource file. getdrawable (R . Drawable.kk_hero);
ImageView ImageView = new ImageView (this);
Imageview.setimagedrawable (drawable);
Layout.addview (ImageView);}}
We see that this activity is very simple, because LinearLayout is a ViewGroup object, so we can dynamically add to it the view control we want, and here we add 8 pictures to it, and we'll look at the effect:
We see that after the activity is started, 8 ImageView objects are generated below it, and these pictures can be scrolled in the vertical direction.
Second, through the ScrollView implementation from the server side to get a news, displayed in the interface
Let's go through ScrollView to do a slightly more practical example, we often use mobile phone to look at the news, of course, a news from the server to get over the data, and there may be a lot of content in the news, so we need to use a scrollable layout to display our news content, and TextView itself can achieve the scrolling display of text, but the combination of ScrollView and TextView can have better results.
Our server is very simple, so that our application access to the server side of an HTML file, we know that the HTML file will have a lot of HTML tags, so if we want to on Android can also display the style of the tag, It's not just about getting the text out of the box, it's going to require an HTML class from Android that handles the HTML string content we get from the server side:
Our layout file is still used just that one:
<?xml version= "1.0" encoding= "Utf-8"?> <scrollview xmlns:android=
"Http://schemas.android.com/apk/res" /android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: Fillviewport= "false" >
<linearlayout
android:id= "@+id/layout"
android:layout_height= "Match_" Parent "
android:layout_width=" wrap_content "
android:orientation=" vertical "/>
</scrollview >
Because you want to access the network, you need to create a new Httputils tool class to get the server-side textual content:
public class Httputils
{
/**
* Access server-side content
* @param path Access URL address
* @param encode encoding
* @ Return returns a string of value
*
/public static string Getdatafromserver (string path, string encode)
{
string result = "";
HttpClient httpclient = new Defaulthttpclient ();
Try
{
HttpPost httppost = new HttpPost (path);
HttpResponse HttpResponse = Httpclient.execute (httppost);
if (HttpResponse!= null && httpresponse.getstatusline (). Getstatuscode () = Httpstatus.sc_ok)
{
result = Entityutils.tostring (Httpresponse.getentity (), "Utf-8");
}
catch (Exception e)
{
e.printstacktrace ();
}
Finally
{
httpclient.getconnectionmanager (). shutdown ();
}
return result;
}
We're still using the previous activity:
public class Scrollviewactivity extends activity {private linearlayout layout;
Private ProgressDialog Dialog;
Private TextView TextView;
Private final String PATH = "http://172.25.152.34:8080/httptest/news.html";
public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.layout_scrollview);
dialog = new ProgressDialog (this);
Dialog.settitle ("hint Information");
Dialog.setmessage ("Loading ...");
Dialog.setcancelable (FALSE);
Dialog.setprogressstyle (Progressdialog.style_spinner);
Layout = (linearlayout) Findviewbyid (r.id.layout);
TextView = new TextView (this);
Layout.addview (TextView);
New MyTask (). Execute (PATH);
public class MyTask extends asynctask<string, void, string> {@Override protected void OnPreExecute ()
{dialog.show (); @Override protected string Doinbackground (String ... params) {string result = Httputils.getdatafromse RVer (params[0], "utf-8");
return result; The Fromhtml method of the @Override protected void OnPostExecute (string s) {//HTML class can handle an HTML string literal so that it can be based on the HTML label
Display its style on the phone spanned spanned = html.fromhtml (s);
Textview.settext (spanned); Set a method for TextView to pass a Linkmovementmethod object in, so that if there is an HREF link in the text, the system will automatically open the browser to jump to the href textview.setmovementmethod (new
Linkmovementmethod ());
Dialog.dismiss ();
}
}
}
Because to access network data, so we need to open a asynctask task, we look at the OnPostExecute method, after getting to the server side of the HTML text content, The Html.fromhtml method we provide via Android can handle our HTML text and translate the HTML tags into the styles we need, but note here that it doesn't deal with all the HTML expressions, such as Let's take a look at the official Android API description of this approach:
public static spanned fromhtml (String source)
Returns displayable styled text from the provided HTML string. Any tags in the HTML'll display as a generic replacement image which your program can then go through and re Place with real images.
This is uses Tagsoup to handle real HTML and including all of the brokenness in the found.
If there is a tag in the text, then this method replaces the picture in our tag with a default image, and we can write a html.imagegetter ourselves to load the picture we want.
Also, because there may be an HREF link in the text content, we can pass Textview.setmovementmethod (new Linkmovementmethod ()); To bind a linkmovementmethod so that when you click on the link, the browser jumps to the link.
Believe that through the previous explanation, we have a further understanding of ScrollView, here and did not speak too much of the horizontalscrollview knowledge, because this is actually and ScrollView is basically the same, but one is the vertical direction of scrolling, And Horizontalscrollview is a horizontal scroll, and the same Horizontalscrollview is also a framelayout, so we usually define a linearlayout child element that is horizontally oriented. So the view child controls that we add inside can be scrolled in a horizontal direction.
Third, summary
This essay mainly explains the knowledge of ScrollView and Horizontalscrollview, because these two layout containers are relatively simple, so basically the concept of things speak less, mainly through the code to understand the use of ScrollView, For Horizontalscrollview, it is used in much the same way that you can learn more about the two controls through the official Android API.
Original link: http://www.cnblogs.com/xiaoluo501395377/p/3460645.html
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.