摇动动画是“滑动”其他小部件android

时间:2012-12-04 17:56:12

标签: android android-layout android-widget android-animation android-xml

我是动画的新手。我在一个按钮上有一个旋转动画。问题是当执行该按钮动画时,按钮与布局中的其他按钮相交。因此在动画期间,由于被其他按钮遮盖,因此无法看到移动按钮。关于如何使我的动画保持在布局的顶层的任何想法?我想找出一个与API 8兼容的解决方案。

//create shake animation
    final Animation shakeIt = AnimationUtils.loadAnimation(this, R.anim.shake);
    findViewById(R.id.button_spin).startAnimation(shakeIt);


// use shake animation
    spinnerButton.startAnimation(shakeIt);

编辑:(这里有更多信息)

所以我在过去的几个小时里玩过它并发现了更多的东西。 Lecho的直觉是正确的,z顺序是一个小部件是否超过"以上的驱动因素。或"以下"另一个。我面临的问题可能是因为两件事:

  1. 位于视图前方的按钮(我想将其向后移动)具有在活动结束时编辑的文本。添加强制窗口小部件重绘的文本是否会更改该视图的Z顺序???

  2. 按钮文本在onResume()中更改,似乎我无法更改该方法中按钮的Z顺序???

  3. 以上2个问题的答案将解决我的问题

    编辑2:有问题的XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg"
        android:orientation="vertical"
         >
    
         <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:orientation="vertical"
           android:layout_weight=".7"
           android:layout_marginTop="5dp" >
    
             <Button
                android:id="@+id/categoryButton"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/drop"
                android:prompt="@string/planet_prompt"
                android:paddingRight="20dp"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textSize="35dp"
                />
    
    
         </LinearLayout>
    
    
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="0.1"
         android:orientation="horizontal" >
    
         </LinearLayout>
    
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:orientation="horizontal"
           android:layout_weight="1.5" >
    
                   <LinearLayout
                   android:layout_width="0dp"
                   android:layout_height="match_parent"
                   android:orientation="horizontal"
                   android:layout_weight="1" >
                   </LinearLayout>
    
                    <Button
                    android:id="@+id/button_spin"
                    android:layout_width="0dp"
                    android:layout_weight="2.35"
                    android:layout_height="fill_parent"
                    android:background="@drawable/letseat"
                    android:layout_gravity="center_horizontal"
                    android:gravity="center" />
    
                  <LinearLayout
                   android:layout_width="0dp"
                   android:layout_height="match_parent"
                   android:orientation="horizontal"
                   android:layout_weight="1" >
                   </LinearLayout>
    
    </LinearLayout>
    
    
    
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="3"
         android:orientation="vertical">
    
          <Button
            android:id="@+id/button_additems"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@drawable/add_rest"
            android:gravity="center_horizontal" />   
    
            <Button
            android:id="@+id/button_showrestaurant"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@drawable/edit_rest"
            android:gravity="center_horizontal" />
    
            <Button
            android:id="@+id/button_nearbyplaces"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:background="@drawable/whats_near"
            android:gravity="center_horizontal" />
    
      </LinearLayout>
    </LinearLayout>
    

1 个答案:

答案 0 :(得分:0)

只是我的想法。在android中,z-order由视图添加到XML的顺序决定。也许动画按钮在另一个之前添加到xml中,因此它更“深”。尝试稍后在XML中放置动画按钮或在布局上使用方法bringChildToFront(buttonView),或者从代码中将按钮添加到布局中。

修改

广告1.添加文字似乎不会更改Z顺序。当我更改文字并致电button.bringToFront()时,Z订单已更改,但在我单独拨打bringToFront()setText()时却没有。

广告2.在onResume中添加文字不会更改Z顺序,但方法bringToFront()似乎按预期工作。

我现在无法与视图动画一起测试这些方法。

测试活动:

public class MainActivity extends Activity {
    private int i = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button btn1 = (Button) findViewById(R.id.btn1);
        final Button btn2 = (Button) findViewById(R.id.btn2);
        btn2.bringToFront();
        final Button btn3 = (Button) findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if ((i % 2) == 0) {
                    final Button btn1 = (Button) findViewById(R.id.btn1);
                    btn1.setText("BUTTON_BLACK_NEW");
                    btn1.bringToFront();
                } else {
                    final Button btn2 = (Button) findViewById(R.id.btn2);
                    btn2.setText("BUTTON_GREY_NEW");
                    btn2.bringToFront();
                }
                ++i;

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        if ((i % 2) == 0) {
            final Button btn1 = (Button) findViewById(R.id.btn1);
            btn1.bringToFront();
        } else {
            final Button btn2 = (Button) findViewById(R.id.btn2);
            btn2.bringToFront();
        }
        ++i;
    }

}

布局:

<RelativeLayout 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=".MainActivity" 
android:id="@+id/main_layout">

<Button
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:text="BUTTON_BLACK"
    android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:text="BUTTON_GREY"
        android:textColor="@android:color/black" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CHANGE_Z"
        android:layout_below="@+id/btn2" />

</RelativeLayout>