How to specify the position of SnackBar in the screen and solve minor problems in Android
- 2021-08-31 09:01:38
- OfStack
Android specifies the location of SnackBar on the screen
Snackbar often appears in the form of a small pop-up box at the bottom of the mobile phone screen or the lower left of the desktop, and is at the top of all layers of the screen. If you want to specify where it appears on the screen, you can place SnackBar in android. support. design. widget. CoordinatorLayout.
Add CoordinatorLayout to RelativeLayout as follows:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/myCoordinatorLayout"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.design.widget.CoordinatorLayout>
Call Snackbar. make () with CoordinatorLayout as the first argument, as follows:
final View viewPos = findViewById(R.id.myCoordinatorLayout);
Snackbar.make(viewPos, R.string.snackbar_text, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_action_undo, showListener)
.show();
You can change the position of Snackbar by changing the position of CoordinatorLayout, such as setting android: paddingBottom = "16dp".
One small problem of Snackbar position display
compile 'com.android.support:design:23.4.0'
The new project of Android Studio will have a method to show Snackbar by default, and 1 is useless. Two days ago, a WeChat official account saw it
http://www.jianshu.com/p/996dc15d21be. It is well written and comprehensive, with basic functions and style customization written.
There is a place where the custom display position is written, which feels necessary, because this is different from Toast, which will be displayed on the soft keyboard, and this will be blocked by the soft keyboard.
ViewGroup.LayoutParams vl = v.getLayoutParams();
CoordinatorLayout.LayoutParams cl = new CoordinatorLayout.LayoutParams(vl.width,vl.height);
cl.gravity = Gravity.CENTER;// Set the display position to center
v.setLayoutParams(cl);
But when the location is set to
Gravity.TOP
Set offset distance but it can't take effect
Rect frame = new Rect();
view.getWindowVisibleDisplayFrame(frame);
l.topMargin =frame.top;
Originally, parent of some View is an inherited FrameLayout. Note that it is necessary to
FrameLayout.LayoutParams l = (FrameLayout.LayoutParams) v.getLayoutParams();
Of course, it is very troublesome to write and display on the top, and it is necessary to deal with displaying and hiding animations
In Snackbar. Java
animateViewIn()
And
animateViewout()
There are rules in it
There is a very good https://github.com/AndreiD/TSnackBar on Github
It is done according to the system, and then a lot of small functions are added. Write down 1 first, and download this to change if the project is used
There is a sentence in design_layout_snackbar. xml
android:layout_gravity="bottom"
Remember to change this 1 to top
So it can be displayed on it.
Then there is the corresponding animation
R.anim.design_snackbar_in
And
R.anim.design_snackbar_out
But both of these are in
Build.VERSION.SDK_INT <Build.VERSION_CODES.ICE_CREAM_SANDWICH// ( 14 )
Called when, so you can delete it without changing it
Just in time to see the system
final void showView()
Method
public void onDismiss(View view)
There is 1 line
view.setVisibility(View.GONE);
But in Tsnackbar, this sentence has been deleted. Well, it seems that we should pay attention to whether there are any other big differences.
Summary: If the project is displayed at the top for a long time, you can use Github, otherwise, you can use design library
Summarize