我正在使用Eclipse编写我的第一个android应用程序。我的应用程序的屏幕显示了一个2x4的按钮表(使用4个线性布局),我有一个方法,通过从一个线性布局中删除按钮并将它们添加到另一个按钮来洗牌。我用过android:animateLayoutChanges =" true"在我的xml中,但这只是导致一些按钮在重新出现在新的位置之前被删除。有没有办法顺利过渡?感谢。
我的xml:
<TableLayout
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:layout_marginStart="0dp"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/ll1"
android:animateLayoutChanges="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btnorange"
android:text="" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btnyellow"
android:text="" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:animateLayoutChanges="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btnblue"
android:text="" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btngreen"
android:text="" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll3"
android:animateLayoutChanges="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btnpink" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btncyan"
android:text="" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll4"
android:animateLayoutChanges="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btnred"
android:text="" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_weight="1"
android:background="@drawable/btnviolet"
android:text="" />
</LinearLayout>
</TableLayout>
我的活动课程:
public class MainActivity extends ActionBarActivity {
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button redo;
TableLayout t1;
LinearLayout l1;
LinearLayout l2;
LinearLayout l3;
LinearLayout l4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1= (TableLayout) findViewById(R.id.table);
l1= (LinearLayout) findViewById(R.id.ll1);
l2= (LinearLayout) findViewById(R.id.ll2);
l3= (LinearLayout) findViewById(R.id.ll3);
l4= (LinearLayout) findViewById(R.id.ll4);
b1= (Button) findViewById(R.id.button1);
b2= (Button) findViewById(R.id.button2);
b3= (Button) findViewById(R.id.button3);
b4= (Button) findViewById(R.id.button4);
b5= (Button) findViewById(R.id.button5);
b6= (Button) findViewById(R.id.button6);
b7= (Button) findViewById(R.id.button7);
b8= (Button) findViewById(R.id.button8);
....
}
public void shuffle(){
l1.removeAllViews();
l2.removeAllViews();
l3.removeAllViews();
l4.removeAllViews();
ArrayList<Button> a=new ArrayList<Button>(8);
a.add(b1);
a.add(b2);
a.add(b3);
a.add(b4);
a.add(b5);
a.add(b6);
a.add(b7);
a.add(b8);
l1.addView(a.remove((int)(Math.random()*8)));
l1.addView(a.remove((int)(Math.random()*7)));
l2.addView(a.remove((int)(Math.random()*6)));
l2.addView(a.remove((int)(Math.random()*5)));
l3.addView(a.remove((int)(Math.random()*4)));
l3.addView(a.remove((int)(Math.random()*3)));
l4.addView(a.remove((int)(Math.random()*2)));
l4.addView(a.remove(0));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
复制以下代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate
android:duration="700"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="-100%"
android:toYDelta="0%" />
</set>
而不是调用removeAllViews()方法
Animation animation = AnimationUtils.loadAnimation(this, R.anim.push_left);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
l1.setVisibility(View.GONE);
}
});