android setText()后组件位置发生改变

money939 2012-05-23 12:41:16
小弟初学android一周……
问题:
我用绝对布局。
在界面上有两个按钮,因为是绝对布局所以我把两个按钮摆在屏幕的中间,不重叠。
我activity里第一秒把button1的位置变一下,用的是button.layout(int l,int t,int r,int b)
下一秒把button2的内容刷新下,用的是button.setText("");

我发现,在第二秒对button2的内容刷新后,button1的会跑到我布局的初始位置……
我想问问如何不要让button1跑到原来的位置。在第二秒我并没有改变button1的layout……
先谢谢大家了


package m.m.m;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

public class test extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Message msg = mHandler.obtainMessage();
msg.what = 1001;
mHandler.sendMessageDelayed(msg, 1000);
}

int k=0;

Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1001:
/*
* 这里是把button1的位置发生改变
* */
Button bt1=(Button)findViewById(R.id.button1);
bt1.layout(20, 20, 50, 50);

Message mg1 = mHandler.obtainMessage();
mg1.what = 1002;
mHandler.sendMessageDelayed(mg1, 1000);
break;
case 1002:
/*
* 这里是把button2的内容发生改变
* */
Button bt2=(Button)findViewById(R.id.button2);
bt2.setText(" "+k);
k++;

Message mg2 = mHandler.obtainMessage();
mg2.what = 1001;
mHandler.sendMessageDelayed(mg2, 1000);
break;
}
super.handleMessage(msg);
}
};
}
...全文
338 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
channan1990 2013-10-10
  • 打赏
  • 举报
回复
大哥,你是怎么解决的,我也映衬到这个问题了。
lover_fs 2013-08-08
  • 打赏
  • 举报
回复
哥们,怎么解决的能否交流下。
money939 2012-05-24
  • 打赏
  • 举报
回复
回复7楼:
非常感谢,受你的启发我知道要怎么解决了,View只会刷新自己所在的布局和父布局,不会刷兄弟布局的!!!跟什么布局没有关系,我已经实现了!!!感谢以上所有回帖的朋友
money939 2012-05-24
  • 打赏
  • 举报
回复
看来跟布局还是有关系啊
money939 2012-05-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
布局文件改了一下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
……
[/Quote]

楼上的大哥,你这么做岂不是把button1放入线性里,那我在java里写 button1.layout(l,t,r,b)不是没有用了吗???刚刚试了真的没有用了,这么设置layout就会以你的linearlayout为基准来设置位置了,如果把线性弄成fill_parent那么弄FrameLayout就没意义了
AMinfo 2012-05-24
  • 打赏
  • 举报
回复
布局文件改了一下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >

<LinearLayout android:layout_width="200px"
android:layout_height="100px"
android:orientation="vertical">

<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" a b"/>

</LinearLayout>

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

<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

</FrameLayout>


为什么会出现这种情况呢:估计是layout方法的调用需要一个稳定的Layout空间,如果所在的Layout空间发生变化了,就会导致layout方法失效..
money939 2012-05-23
  • 打赏
  • 举报
回复
其实我用的是绝对布局,,但是改成上面的单针布局也不行,老师原来说改单针,可以,我试了下不行……
以下是我的绝对布局的main

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ly"
>
<Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_x="26dip" android:layout_y="121dip"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:layout_x="230dip" android:layout_y="121dip"></Button>
</AbsoluteLayout>
money939 2012-05-23
  • 打赏
  • 举报
回复
感谢大家回帖:
以下是main文件

我的问题是,button1原来在位置1,在第一秒我修改了其位置改到了位置2,在第二秒的时候我没有修改其位置,只是修改了button2的text,然而button1却自己跑到了位置1,这是问什么,如何避免???
感激不尽

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/ly"
>
<Button android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_x="26dip" android:layout_y="121dip"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:layout_x="230dip" android:layout_y="121dip"></Button>
</FrameLayout>
huanai2000 2012-05-23
  • 打赏
  • 举报
回复
同学,没有看出什么问题啊,把布局文件也贴出来吧。
逐Ls梦 2012-05-23
  • 打赏
  • 举报
回复
用线程
AMinfo 2012-05-23
  • 打赏
  • 举报
回复
上布局文件,再上图吧
money939 2012-05-23
  • 打赏
  • 举报
回复
小弟一直在等答案

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧