Mam pogląd złożony z TableLayout, TableRow and TextView. Chcę, żeby to wyglądało jak siatka. Muszę uzyskać wysokość i szerokość tej siatki. Metody getHeight()i getWidth()zawsze zwracają 0. Dzieje się tak, gdy dynamicznie formatuję siatkę, a także gdy używam wersji XML.
Jak odzyskać wymiary widoku?
Oto mój program testowy, którego użyłem w Debugowaniu do sprawdzenia wyników:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TextView;
public class appwig extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maindemo); //<- includes the grid called "board"
int vh = 0;
int vw = 0;
//Test-1 used the xml layout (which is displayed on the screen):
TableLayout tl = (TableLayout) findViewById(R.id.board);
tl = (TableLayout) findViewById(R.id.board);
vh = tl.getHeight(); //<- getHeight returned 0, Why?
vw = tl.getWidth(); //<- getWidth returned 0, Why?
//Test-2 used a simple dynamically generated view:
TextView tv = new TextView(this);
tv.setHeight(20);
tv.setWidth(20);
vh = tv.getHeight(); //<- getHeight returned 0, Why?
vw = tv.getWidth(); //<- getWidth returned 0, Why?
} //eof method
} //eof class
getHeight()a getWidth()po zakończeniu cyklu życia Aktywności poprosił opinie, żeby się zmierzyły, innymi słowy, robią tego rodzaju rzeczy onResume()i to wszystko. Nie należy oczekiwać, że jeszcze nie rozplanowany obiekt pozna jego wymiary, to cała sztuczka. Nie ma potrzeby używania magii sugerowanej poniżej.
getHeight()/Width()w onResume()nie dały> 0 wartość dla mnie.