The center or bottom of an activity that Android develops back to the top

Source: Internet
Author: User

When we browse the details of Taobao products, we will get back to the top of this feature, let's talk about the simple implementation of this feature


<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"
Android:layout_width= " Match_parent "
android:layout_height=" match_parent
<scrollview
android:id= "@+id/sc"
Android : layout_width= "match_parent"
android:layout_height= "wrap_content"
android:fillviewport= "true"
<linearlayout
android:layout_width= "match_parent"
android:layout_height= "match_parent"
Android:o rientation= "vertical"
<webview
android:id= "@+id/wb"
android:layout_width= "Match_parent"
android:layout_height= "Match_parent"

</webview>
</linearlayout>
</scrollview
 
<button
android:id= "@+id/bt"
android:layout_width= "match_parent"
Android:layout_ height= "50DP"
android:background= "@android: Color/black"
android:layout_gravity= "Bottom"
android:text= "Point me Up"
android:textcolor= "#ffffff"
/>
 
</framelayout>

Layout of the outermost use framelayout, and then set on the ScrollView, but must pay attention to ScrollView high can only be wrap_content, or click Back to the bottom no effect.

The code is as follows

public class Mainactivity extends Appcompatactivity {
Private ScrollView SC;
Private WebView WB;
Private Button BT;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Sc= (ScrollView) Findviewbyid (R.id.sc);
wb= (WebView) Findviewbyid (R.ID.WB);
bt= (Button) Findviewbyid (R.ID.BT);
Bt.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Sc.post (New Runnable () {
@Override
public void Run () {
Sc.post (New Runnable () {
public void Run () {
Back to the top of
Sc.fullscroll (SCROLLVIEW.FOCUS_UP);
}
});
}
});
}
});
Initview ();
}

private void Initview () {
String url = "https://www.111cn.net/";
WebSettings websettings = Wb.getsettings ();
Websettings.setlayoutalgorithm (WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
Websettings.setjavascriptenabled (TRUE); To set the WebView property to execute a JavaScript script
Setting Access to Files
Websettings.setallowfileaccess (TRUE);
Websettings.setbuiltinzoomcontrols (false);//setting whether the Zoom tool is displayed
Wb.loadurl (URL);
Wb.setwebviewclient (New Webviewclient ());

}
}


Implementation of the effect as shown, I am here to use WebView link Baidu Web page, other controls are the same

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.