我发起的活动有两个按钮组成的线性布局。两个按钮都有监听器:第一个按钮(b)在点击时自动移动:30px到左边,30px返回到下一个点击。 第二个(b2)在点击后更改其文本。下面是代码:View.layout()的作品,直到下一个UI更新
public class TestActivity extends Activity {
public final String TAG="TestActivity";
boolean toTop=true;
boolean setInitialText=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b=(Button)findViewById(R.id.button);
Button b2=(Button)findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int modifier;
if(toTop) modifier=-30;
else modifier=30;
v.layout(v.getLeft()+modifier,v.getTop(),v.getRight()+modifier,v.getBottom());
toTop=!toTop;
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String currentText;
if(setInitialText)currentText="Press to change text";
else currentText="Press to change back";
((Button)v).setText(currentText);
setInitialText=!setInitialText;
}
});
}
}
XML布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to begin animation" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press to change text" />
我的问题:当B移动到左边,我按B2,B移动到初始位置。为什么?我不希望它回退,也没有在任何地方指定它。
它看起来像View.layout失去其作用。为什么会发生?我在其他情况下测试过,似乎任何UI更新都会使所有调用的View.layout方法失去效果。
在我的主要项目有它填充了图片来自背景的ListView - 所有视图时,新的形象出现松动移动效果。此外,如果我添加EditText并尝试输入某些内容(如用户),则视图也会松动其效果。任何人都可以向我解释发生了什么事情,为什么意见会回退?