View.layout()的作品,直到明年UI更新 [英] View.layout() works until next UI update

查看:152
本文介绍了View.layout()的作品,直到明年UI更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发起活动包括线性布局与两个按钮。这两个按钮有听众:第一个(B)移动本身​​在点击:30PX向左和30像素背在接下来的点击。
第二个(B2)的变化在点击其文本。这里是code:

My launching activity consists of Linear layout with two buttons. Both buttons have listeners: the first one (b) moves itself upon click: 30px to the left and 30px back upon the next click. The second one (b2) changes its text upon click. Here is the code:

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 layout-file:

<?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被移动到左和我preSS B2,B移动到其初始位置。为什么?我不希望它搬回,并没有任何地方指定。

My problem: when b is moved to the left and I press b2, b moves to its initial position. Why? I don't want it to move back and didn't specify it anywhere.

它看起来像View.layout失去其作用。为什么会发生?我测试了在其他情况下,似乎任何UI更新使所有调用的方法View.layout松散的效果。

It looks like View.layout looses its effect. Why it happens? I tested that in other situations and it seems that any UI update makes all invoked View.layout methods loose their effect.

在我的主要项目有它填充了图片来自背景的一个ListView - 所有视图时,新的形象出现松动的移动效果。此外,如果我想补充的EditText并尝试输入一些(如用户),动作来看松散的功效。谁能给我解释一下这是怎么回事,为什么观点回迁?

In my main project there is a ListView which is populated with images from background - all view moves loose effect when new image appears. Besides, if I add EditText and try to enter something (as user), view moves loose their effect as well. Can anyone explain to me what's going on and why do views move back?

推荐答案

<一个href=\"http://stackoverflow.com/questions/10137657/onclicklistener-issues-after-imagebutton-moved-after-translateanimation\">Here说,这个问题可能会得到解决,使用 view.setLayoutParams()而不是 view.layout()

Here says that this issue may be solved, using view.setLayoutParams() instead of view.layout()

这篇关于View.layout()的作品,直到明年UI更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆