簡體   English   中英

我無法更改ImageView和TextView的setText的位置

[英]I can't change position of ImageView and setText of TextView

我有以下代碼(適用於Android):

handler.post(new MyRunnable());
....
static public class MyRunnable implements Runnable {
        public void run() {
            text.setText("Hi");
            imageV.layout(imageV.getLeft()+y, 218, imageV.getLeft()+150+y, 464);
            }
       }

設置了文本,但imageV位置未更改。 當我不寫text.setText("Hi"); ,位置將改變。 我有一個AbsoluteLayout

您需要使用setLayoutParams

handler.post(new MyRunnable());
....
static public class MyRunnable implements Runnable {
    public void run() {
        text.setText("Hi");
        final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) imageV.getLayoutParams();
        params.left += y;
        params.top = 218;
        params.right = params.left + 150;
        params.bottom = 464;
        imageV.setLayoutParams(params);
        }
 }

請記住,至少經過一次布局傳遞后,您才能使用view.getLeft()等。 在此之前,它將返回0。如果要通過不會做的布局進行操作。 您還必須調用requestLayout()。 但是無論如何,調用layout + requestLayout()幾乎是setLayoutParams的實現,因此最好改用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM