Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
先看一下Android懸浮按鈕點(diǎn)擊回到頂部的效果:

FloatingActionButton是Design Support庫中提供的一個控件,這個控件可以輕松實(shí)現(xiàn)懸浮按鈕的效果
首先,要在項目中使用這個懸浮按鈕就要先把design這個包導(dǎo)入項目
gradle中加入依賴
compile 'com.android.support:design:25.0.0'
接下來就是在xml中使用:
我這里是放置一個listView模擬返回頂部
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ListView
android:id="@+id/listview_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/floating_btn_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="@mipmap/top"
app:elevation="10dp"
android:layout_margin="15dp"/>
</RelativeLayout>
其中 app:elevation=”10dp”是給FloatingActionButton指定一個高度,高度越高,投影的范圍越大,但是投影效果越淡,反之則反
接下來是MainActivity中的使用:
package com.duanlian.floatingbtn;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private FloatingActionButton mFloatBtn;
private MyListViewAdapter mAdapter;
private List<String> mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mListView = (ListView) findViewById(R.id.listview_main);
mFloatBtn = (FloatingActionButton) findViewById(R.id.floating_btn_main);
mList = new ArrayList<>();
for (int i = 0; i < 30; i++) {
mList.add(i + "");
}
mAdapter = new MyListViewAdapter(this, mList);
mListView.setAdapter(mAdapter);
//懸浮按鈕的點(diǎn)擊事件的監(jiān)聽
mFloatBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//listView返回到頂部
mListView.smoothScrollToPosition(0);
}
});
}
}
怎么樣,簡單吧。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)之FloatingActionButton懸浮按鈕基本使用、字體、顏色用法示例
- Android自定義可拖拽的懸浮按鈕DragFloatingActionButton
- Android 中FloatingActionButton(懸浮按鈕)實(shí)例詳解
- Android中FloatingActionButton實(shí)現(xiàn)懸浮按鈕實(shí)例
- Android仿知乎懸浮功能按鈕FloatingActionButton效果
- Android開發(fā)懸浮按鈕 Floating ActionButton的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)懸浮可拖拽的Button
相關(guān)文章
Android動態(tài)權(quán)限申請實(shí)現(xiàn)步驟分解
對于一些危險權(quán)限在AndroidManifest清單文件中申請之后,還需要得到用戶的許可并打開,才算是真正的開啟了這個權(quán)限。所以可以使用動態(tài)申請權(quán)限,對于某個功能,如果需要開啟某個權(quán)限,在用戶使用它之前,彈窗提示用戶是否要開啟這個權(quán)限2023-04-04
Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能示例
這篇文章主要介紹了Android開發(fā)之TextView使用intent傳遞信息,實(shí)現(xiàn)注冊界面功能,涉及Android使用intent傳值及界面布局等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04
Android編程之手機(jī)壁紙WallPaper設(shè)置方法示例
這篇文章主要介紹了Android編程之手機(jī)壁紙WallPaper設(shè)置方法,結(jié)合實(shí)例形式分析了Android手機(jī)壁紙WallPaper的相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2017-08-08
Android?XML數(shù)據(jù)解析要點(diǎn)介紹
這篇文章主要為大家介紹了Android?XML數(shù)據(jù)解析要點(diǎn)介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Android實(shí)現(xiàn)標(biāo)題顯示隱藏功能
這篇文章主要介紹了Android實(shí)現(xiàn)標(biāo)題顯示隱藏功能2016-02-02
Android Studio配置(Android Studio4.1為例)
這篇文章主要介紹了Android Studio配置(Android Studio4.1為例),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
36個Android開發(fā)常用經(jīng)典代碼大全
本篇文章主要介紹了36個Android開發(fā)常用經(jīng)典代碼片段,都是實(shí)用的代碼段,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11
Android開發(fā)兩個activity之間傳值示例詳解
這篇文章主要為大家介紹了Android開發(fā)兩個activity之間傳值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

