当二级菜单弹出时,界面的空间可能会不够或变得拥挤。
解决:
这时可以将ToolBar和界面其它View上移,
这样因为二级菜单的弹出而让界面拥挤的问题就得到了解决。
程序效果如图所示:
MainActivity.java
public class MainActivity extends AppCompatActivity { private Toolbar mToolbar; private ImageView mImageView; private FrameLayout firstbar_layout,secondbar_layout; private int animationDuration = 300; private int animator_height,secondbar_height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); //Toolbar mImageView = (ImageView)findViewById(R.id.image); //占用屏幕空间的视图,这里用了图片来代替 firstbar_layout = (FrameLayout)findViewById(R.id.firstbar); //一级菜单 secondbar_layout = (FrameLayout)findViewById(R.id.secondbar); //二级菜单 animator_height = ScreenInfoUtil.dip2px(this, 50); //界面中Toolbar和图片需要上移的高度 secondbar_height = ScreenInfoUtil.dip2px(this, 100); //二级菜单的高度 firstbar_layout.setOnClickListener(new View.OnClickListener() { //一级菜单点击事件 @Override public void onClick(View view) { moveY(0, -animator_height); moveBar(secondbar_height,0); } }); secondbar_layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //二级菜单点击事件 moveY(-animator_height, 0); moveBar(0,secondbar_height); } }); } private void moveY(float startY, float endY) { //利用ObjectAnimator实现视图的Y轴移动操作 Object[] animComponent = {mToolbar,mImageView}; //对Toolbar和ImageView进行移动 for (final Object obj : animComponent) { ObjectAnimator animator = ObjectAnimator.ofFloat(obj, "translationY", startY, endY); animator.setDuration(animationDuration).setInterpolator( new AccelerateDecelerateInterpolator()); animator.start(); } } private void moveBar(float startY, float endY) { //利用ObjectAnimator实现二级菜单的弹出操作 secondbar_layout.setVisibility(View.VISIBLE); Object[] animComponent = {secondbar_layout}; for (final Object obj : animComponent) { ObjectAnimator animator = ObjectAnimator.ofFloat(obj, "translationY", startY, endY); animator.setDuration(animationDuration).setInterpolator( new AccelerateDecelerateInterpolator()); animator.start(); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/root_layout" tools:context="com.lla.testviewup.MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="50dp" android:background="?attr/colorPrimary"/> <ImageView android:layout_width="match_parent" android:layout_height="280dp" android:id="@+id/image" android:layout_marginTop="120dp" android:scaleType="centerCrop" android:background="@drawable/godness"/> <FrameLayout android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/firstbar" android:layout_alignParentBottom="true" android:background="@color/colorPrimary" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="我是一级菜单,点我弹出二级菜单"/> </FrameLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/secondbar" android:layout_alignParentBottom="true" android:background="@color/bottom_bar" android:visibility="invisible"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:text="我是二级菜单,点我返回一级菜单"/> </FrameLayout> </RelativeLayout>