android - 如何创建动画扩展菜单

标签 android android-animation android-view

我想创建动画菜单,如附图所示。

enter image description here

当点击圆形按钮(带有向上箭头)时,它将向上扩展(如第二张图片所示)。注意:图像未按比例绘制,请原谅我。

enter image description here

我的粗略想法是这样的:

  • 在展开的菜单中,第一行和第二行具有相同的 View
    类型,所以我将为该 View 创建一个布局文件
  • 第三行将 是另一种布局。
  • 使用容器(FrameLayout)来承载回合 ImageButton(带箭头的按钮)
  • Activity 的
  • onCreate, 给容器充气,添加上述所有 View ,但设置仅可见 到第一行和圆形按钮。
  • 当圆形按钮处于 切换后,只需设置其他 View 可见/不可见。

我的问题:

  • 我的方法有什么问题吗?有好的教程吗?
  • 容器应该是 FrameLayout 还是其他?
  • 在设置其他 View 可见/不可见时,如何创造真正展开和折叠的效果?

如果问题含糊不清,我们深表歉意。谢谢。

最佳答案

您可以使用Sliding Drawer

灵感来自example here ..

这是完整的代码..

public class SlidingDrawerActivity extends Activity implements OnClickListener {
Button slideButton,b1, b2,b3;
SlidingDrawer slidingDrawer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_drawer);
slideButton = (Button) findViewById(R.id.slideButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);
b1 = (Button) findViewById(R.id.Button01);
b2 = (Button) findViewById(R.id.Button02);
b3 = (Button) findViewById(R.id.Button03);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
slideButton.setText("V");
}
});
slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
slideButton.setText("^");
}
});
}
@Override
public void onClick(View v) {
Button b = (Button)v;
Toast.makeText(SlidingDrawerActivity.this, b.getText() + " is Clicked :)", Toast.LENGTH_SHORT).show();
}
} 

activity_sliding_drawer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Drag the control at the bottom"
android:textSize="20dp"
tools:context=".SlidingDrawerActivity" />

<SlidingDrawer
android:layout_alignParentBottom="true"
android:id="@+id/SlidingDrawer"
android:layout_width="wrap_content"
android:layout_height="250dip"
android:content="@+id/contentLayout"
android:handle="@+id/slideButton"
android:orientation="vertical"
android:padding="10dip" >

<Button
android:id="@+id/slideButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="^"
>
</Button>

<LinearLayout
android:id="@+id/contentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >

<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Button 1" >
</Button>

<Button
android:id="@+id/Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Button 2" >
</Button>
<Button
android:id="@+id/Button03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Button 3" >
</Button>
</LinearLayout>
</SlidingDrawer>

</RelativeLayout>

关于android - 如何创建动画扩展菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18844541/

相关文章:

android - Sip 管理器因错误注册超时而失败(错误代码为 5)

Android .xml设计

android - 在服务中调用 getSystemService

android - 在 Activity 过渡动画期间保持 Action Bar 稳定

安卓循环定时器

安卓蓝牙 ACTION_DISCOVERY_FINISHED 不工作

android - 在列表已经更新和绘制后,如何为单个项目(条目)设置动画?

android - 在自定义 View 中实现振动器

android - 如何获取对应于整个屏幕的屏幕坐标,而不是触摸的 View ?

android - MediaController 定位 - 绑定(bind)到 VideoView