键盘上/下时动画LinearLayout

时间:2015-09-22 03:46:43

标签: android android-layout android-activity android-xml

我有这样的布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="25dp"
    android:paddingRight="25dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:background="@drawable/logo" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView ... />

            <EditText ... />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView ... />

            <EditText ... />

        </LinearLayout>

    </LinearLayout>

    <Button ... />

</LinearLayout>

我希望淡出ImageView并在键盘出现时将第二个(内部)LinearLayout向上移动(靠近屏幕顶部),然后在键盘返回时执行相反的操作下来。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

使用 TranslateAnimation 类为视图设置动画。

Animation slide = new TranslateAnimation(fromX,toX,fromY,toY);
slide.setDuration(300);//here you can set your own duration of animation in ms.
yourView.startAnimation(slide);

您也可以覆盖一些回调。

slide.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }
        @Override
        public void onAnimationEnd(Animation animation) {

        }
        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

没有直接的方法可以找出android软键盘是否为opne。但是你可以解决一下你的活动视图根和窗口大小之间的大小差异。有很多方法可以做到这一点,我发现的一种方式是:

    final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
        if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
            ... do something here
        }
     }
});
相关问题