我期待在创造一个最佳的方式手风琴式的小部件,如这个网页上 。 有没有使用标准的Android工具包实现相同效果的一种方式或是否需要建立自定义窗口小部件? 如果是这样的 - 这一个,你会建议,如果任何延长?
Answer 1:
我已经推Android的手风琴视图在github上的项目。 我们用它为我们的客户,在2.2,2.3.x版本,3.2和4.0.3进行测试。 为我们工作很不错。
要添加动画的折叠/展开的下一个步骤。
这里是小屏幕截图:
Answer 2:
而如果你仍然不知道 - 这可以通过对按键/布局堆叠线性布局内的被几乎完成。 伪代码如下
<LinearLayout android:orientation="vertical">
<Button android:text="Panel 1"/>
<SomeKindOfLayout android:id="@+id/panel1">
<!-- widgets in first panel go here -->
</SomeKindOfLayout>
<Button android:text="Panel 2"/>
<SomeKindOfLayout android:id="@+id/panel2" android:visibility="gone">
<!-- widgets in second panel go here -->
</SomeKindOfLayout>
<Button android:text="Panel 3"/>
<SomeKindOfLayout android:id="@+id/panel3" android:visibility="gone">
<!-- widgets in third panel go here -->
</SomeKindOfLayout>
<Button android:text="Panel 4"/>
<SomeKindOfLayout android:id="@+id/panel4" android:visibility="gone">
<!-- widgets in fourth panel go here -->
</SomeKindOfLayout></LinearLayout>
另一件事可能尝试是在彼此的顶部堆叠ExpandableListView-S
Answer 3:
我有我自己的风险,并说,@Bostone答案是最合适的。 我下面给你,所以你可以看到我的代码。 感谢GitHub的马切伊Łopaciński,但@SudoPlz在评论中说,没有任何文件在所有。 我不知道从哪里开始。 我试着用Eclipse和Android Studio来做到这一点,既不我可以运行该项目,开始盘算着如何工作。 此外,去年在该项目承诺是大约一年前,至极让我担心了很多,如果出了问题,我会在一个死胡同被卡住。 所以,毫不拖延地,这里是总部设在@Bostone我的解决方案:
1.-首先,你必须创建一个按钮,并嵌套在一个滚动型布局:
<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
了。 这个问题有关于一个完美的事情,解释缩放动画
Answer 4:
在我的应用程序中,我使用Riya的Gayasen手风琴视图。 这是非常容易上手并运作良好。 下面是我做的。
- 从下载的源代码中的zip https://github.com/riyagayasen/Android_accordion_view 。
- 解压,复制“easyaccordion”文件夹到你项目的根目录(包含“应用程序”文件夹的目录)
- 在你的项目根settings.gradle文件添加:easyaccordion。 下面是我的文件看起来像:
include ':app',':easyaccordion'
- 在您的应用程序/文件的build.gradle中添加以下行的依赖块。
implementation 'com.riyagayasen.android:easyaccordion:1.0.3'
- 仅此而已。 你可以开始使用你的布局内的组件。