簡體   English   中英

如何在ANDROID中獲取表格布局單元的位置

[英]How to get location of table layout cell in ANDROID

我在表布局中添加帶有textview的動態行,而且我還希望該textview的xy坐標。 為此,我使用了view.getLocationOnScreen(loc)方法。

但是只顯示最后一個項目的坐標,我想顯示所有項目的坐標。

這是我的源代碼,請為此提供幫助。

謝謝。

public class MainActivity extends Activity {
TextView tv1, tv2;
TableRow row;
TextView txt;
TableLayout tl;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tl = (TableLayout) findViewById(R.id.tableLayout1);
    for (int i = 0; i <= 15; i++) {
        row = new TableRow(this);
        txt = new TextView(this);

        tl.addView(row);
        row.addView(txt);
        readLocation();
    }
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    readLocation();
}

private void readLocation() {
    int[] locationOnScreen = new int[2];
    txt.getLocationOnScreen(locationOnScreen);
    txt.setText(locationOnScreen[0] + " : " + locationOnScreen[1]);
}

}

和我下面的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TableLayout>
</ScrollView>

您不應該嘗試以這種方式在onCreate方法中獲取那些坐標,因為此時布局過程尚未完成。

這是根據您的代碼進行了一些修改的演示:

public class MainActivity extends Activity {

ArrayList<TextView> txt = new ArrayList<TextView>();
TableLayout tl;

Handler _h = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tl = (TableLayout) findViewById(R.id.tableLayout1);
    for (Integer i = 0; i <= 15; i++) {
        TableRow therow = new TableRow(this);
        TextView thetxt = new TextView(this);

        therow.addView(thetxt);
        tl.addView(therow);

        txt.add(thetxt);

        thetxt.setText("stub");
    }

    tl.requestLayout();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
protected void onResume() {
    super.onResume();
    _h.postDelayed(new Runnable() {

        @Override
        public void run() {

            for (Integer i = 0; i <= 15; i++) {

                TextView thetxt = txt.get(i);

                int[] locationOnScreen = new int[2];
                thetxt.getLocationOnScreen(locationOnScreen);
                thetxt.setText(locationOnScreen[0] + " : " + locationOnScreen[1]);
            }        


        }
    }, 1000);

}

}

關鍵點是onResume中的postDelayed ,可讓系統正確postDelayed所有視圖。

暫無
暫無

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

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