我在以下代码中遇到两个问题。这段代码应该提供一个包含左,右和下三个选项的溢出菜单。他们必须使用默认动画相应地移动按钮。第一个问题是TransitionManager根本不起作用。按钮移动时没有任何动画。第二个问题是,当按钮向左和向右移动时,它会在操作栏后面丢失,我该怎么办呢?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Button button = (Button)findViewById(R.id.button);
RelativeLayout main_view = (RelativeLayout) findViewById(R.id.main_view);
switch(item.getItemId()){
case R.id.right:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesRight = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesRight.addRule(RelativeLayout.ALIGN_PARENT_TOP);
positionRulesRight.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(positionRulesRight);
return true;
case R.id.left:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesLeft = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesLeft.addRule(RelativeLayout.ALIGN_PARENT_TOP);
positionRulesLeft.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setWidth(250);
button.setHeight(250);
button.setLayoutParams(positionRulesLeft);
return true;
case R.id.down:
if(item.isChecked()){
item.setChecked(false);
}
else
item.setChecked(true);
TransitionManager.beginDelayedTransition(main_view);
RelativeLayout.LayoutParams positionRulesDown = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
positionRulesDown.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
positionRulesDown.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(positionRulesDown);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.revolise.animations.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main"
android:id="@+id/include" />
<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="70dp"
android:text="@string/button_text"/>
<RelativeLayout
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>