我不得不冒险说@Bostone 的答案是最合适的。我将在下面给您我的代码,以便您查看。感谢 GitHub Maciej Łopaciński,但正如 @SudoPlz 在 cmets 中所说,根本没有文档。我不知道从哪里开始。我尝试使用 Eclipse 和 Android Studio 来做这件事,但我都无法运行该项目来开始弄清楚它是如何工作的。此外,该项目的最后一次提交是大约 1 年前,这让我非常担心如果出现问题我会陷入死胡同。因此,事不宜迟,这是我基于@Bostone 的解决方案:
1.- 首先你必须创建一个 Button 和一个嵌套在 ScrollView 中的布局:
<ScrollView 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"
tools:context=".TasksListActivity">
<Button
android:id="@+id/magic_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Magic Btn"/>
<LinearLayout
android:id="@+id/magic_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
</ScrollView>
2.- 之后去你对应的JAVA,找到按钮并设置一个onClickListener,在onClickListener里面你会找到布局。
Button findMagicBtn = (Button) findViewById(R.id.magic_btn);
findMagicBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
3.- 现在我们将在 onClickListener 中找到布局。正如您在第一步中看到的,布局默认设置为GONE,但现在我们必须将其设置为可见。问题是,我怎样才能让它再次消失,反之亦然?因此,我们将添加一个条件来获取布局的可见性,并在此基础上将其隐藏或显示:
Button findMagicBtn = (Button) findViewById(R.id.magic_btn);
findMagicBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout findMagicLl = (LinearLayout) findViewById(R.id.magic_layout);
if (findMagicLl.getVisibility() == View.VISIBLE) {
findMagicLl.setVisibility(View.GONE);
} else {
findMagicLl.setVisibility(View.VISIBLE);
}
}
});
这可以随意叠加。
所以现在如果你真的想让它看起来像一个手风琴,你可以拉个皮条。您可以在其右侧放置一个箭头,例如:
findMagicBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.right_arrow, 0);
您应该在 onClickListener 中执行此操作,并使用条件来更改箭头的方向,通过更改可绘制对象(如果已消失和向下箭头,如果可见则向上箭头)。
此外,您可以通过添加动画使其看起来更自然,如下所示:
ScaleAnimation animation = new ScaleAnimation(1f, 1f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(180);
animation.setFillAfter(true);
findMagicLl.startAnimation(animation);
您必须考虑到上述动画必须在视图可见时完成。请注意setFillAfter(true),将永久更改容器的大小,因此如果您这样做,也许您不想再使用VIEW.GONE。这个问题对Scale Animation有一个精彩的解释