??????? 懸浮按鈕是懸浮于界面之上,滑動(dòng)屏幕時(shí)按鈕不會(huì)跟隨屏幕移動(dòng),它是Design Support庫(kù)中提供的一個(gè)控件,這個(gè)控件可以幫助我們輕松實(shí)現(xiàn)懸浮按鈕效果。
FloatingActionButton實(shí)現(xiàn)
??????? 下面實(shí)現(xiàn)一個(gè)發(fā)布的懸浮按鈕。
?????? 由于需要用到floatingActionButton,所以先導(dǎo)入,可參考這篇文章:
『Android基礎(chǔ)入門』懸浮按鈕 - 掘金
??????? 準(zhǔn)備一個(gè)圖標(biāo),放置到drawable目錄下。
??????? 修改activity_main.xml代碼,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:fab_size="normal"
android:src="@drawable/publish" />
</FrameLayout>
</LinearLayout>
MainActivity代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "點(diǎn)擊了按鈕", Toast.LENGTH_SHORT).show();
}
});
}
}
效果如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-647343.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-647343.html
到了這里,關(guān)于【Android入門到項(xiàng)目實(shí)戰(zhàn)-- 11.1】—— 實(shí)現(xiàn)懸浮按鈕的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!