【问题标题】:Animate a linear layout in android在android中为线性布局设置动画
【发布时间】:2013-12-30 01:51:16
【问题描述】:

我是 android 新手,正在尝试在原生 android 中创建应用程序。

我有一个顶栏,它是整个屏幕的 10% 高度和全宽。最初我想在应用程序启动时显示该栏。当用户向下拖动应用程序时,需要将“顶栏”设置为-10%,使用户无法查看。此外,当用户向上拖动屏幕时,需要将“顶栏”设置为 0,即屏幕的开头(正常位置)。

所以我的布局是这样的

<relativelayout>
<linearLayout>
// Top bar content here 
<linearLayout>


<linearLayout>

// With list items, etc

</linearLayout>


</relativelayout>

我被那个动画部分困住了。如何获取事件并制作动画。

请给我一个提示。

提前致谢

【问题讨论】:

标签: android android-linearlayout android-animation


【解决方案1】:

您可以创建一个 onTouch() 方法,其中 View 是整个屏幕。根据触摸动作指定您希望顶部栏执行的操作。下面是一些代码:

public boolean onTouch(View v, MotionEvent evt) {
    switch(evt.getAction()){
        case MotionEvent.ACTION_DOWN:
            // your code that makes it disappear
            return true;
        case MotionEvent.ACTION_UP:
            // your code that makes it reappear
            return true;
    }
    return false;
}

在您的 xml 中,将 id 添加到您的布局中。

<LinearLayout 
 android:id="my_layout"
 android:layout_height="fill_parent"
 android:layout_width="fill_parent">
   <TextView
    android:id="topbar"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_weight="1">
   <ListView
    android:id="list"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_weight="9">
</LinearLayout>

id 将是您在 onTouch() 方法中调用的,用于将布局权重或顶部栏的百分比从 1 更改为 0,反之亦然,由您自行决定。

查看the first answer 的算法来确定触摸事件是向上还是向下滚动。

【讨论】:

  • 嗨筹码感谢您的评论。但你能帮我如何将线性布局设置为 -10% 吗?
  • 现在我正在查看您的 xml 代码,我不确定这是否是解决您问题的最佳方法。根据您所拥有的,我建议您使用单一布局(相对或线性)并为页面上的各种元素使用 TextView 和 ListView。
猜你喜欢
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多