-
效果如下:
20170207111422954.png -
引入CoordinatorLayout、FloatingActionButton、Snackbar
```
compile 'com.android.support:design:25.1.0'
```
- 布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.hnshangyu.coordinatorlayout.MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher" />
</android.support.design.widget.CoordinatorLayout>
CoordinatorLayout作为“super-powered FrameLayout”,设置子视图的Android:layout_gravity属性控制位置。
- 在activity中:
package cn.hnshangyu.coordinatorlayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Snackbar.make(view,"弹出来了,哈哈",Snackbar.LENGTH_INDEFINITE)
// .show();
Snackbar.make(view,"弹出来了,哈哈",Snackbar.LENGTH_INDEFINITE)
.setAction("取消", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"点击消除Action后的响应事件",Toast.LENGTH_LONG)
.show();
}
})
.show();
}
});
}
}
如果想使用FloatingActionButton与ListView, RecyclerView 和 ScrollView等一起使用,请看:http://blog.csdn.net/huangxiaoguo1/article/details/53708112
网友评论