[英]Getting the width/height of a layout in Android
我想知道如何測量視圖的尺寸。 就我而言,它是絕對布局。 我已經閱讀了有關這些問題的答案,但我仍然沒有得到它。
這是我的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);
drawOval();
}
public void drawOval(){ //, int screenWidth, int screenHeight){
AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);
int screenWidth = layoutbase.getWidth();
int screenHeight = layoutbase.getHeight();
Log.i("MyActivity", "screenWidth: " + screenWidth + ", screenHeight: " +screenHeight);
Coordinates c = new Coordinates(BUTTONSIZE,screenWidth,screenHeight);
...some code ...
((ViewGroup) layoutbase ).addView(mybutton, new AbsoluteLayout.LayoutParams(BUTTONSIZE, BUTTONSIZE, c.mX, c.mY));
mybutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showText(mybutton);
}
});
}
public void showText(View button){
int x = findViewById(LAYOUT).getWidth();
int y = findViewById(LAYOUT).getHeight();
Toast message = Toast.makeText(this, "x: " + x , Toast.LENGTH_SHORT);
message.show();
}
該getWidth()
命令的偉大工程showText()
但它沒有做drawOval()
我知道它看起來有點不同但我也使用了int x = findViewById(LAYOUT).getWidth();
drawOval()
版本,x / y始終為0
。 我真的不明白為什么在前一點似乎沒有寬度/高度。 即使我實際在Absolute Layout上繪制一個Button, getWidth()
也會返回0
。 顯然,我想測量drawOval()
的大小。
我想會幫助你。
LinearLayout headerLayout = (LinearLayout)findviewbyid(R.id.headerLayout);
ViewTreeObserver observer = headerLayout .getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// TODO Auto-generated method stub
int headerLayoutHeight= headerLayout.getHeight();
int headerLayoutWidth = headerLayout.getWidth();
headerLayout .getViewTreeObserver().removeGlobalOnLayoutListener(
this);
}
});
}
getWidth()
給出0,因為在布局實際發生之前調用onCreate
。 由於視圖能夠基於屬性或其他元素(例如fill_parent
)具有動態位置和大小,因此對於任何給定視圖或布局,沒有固定大小。 在運行時,有一個時間點(實際上它可以根據許多因素重復發生),其中一切都是實際測量和布局的。 如果你真的需要高度和寬度,你必須在以后發現它們。
這特別處理了Dimension
的問題
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
這可以幫助您管理維度。
注意:這將返回顯示尺寸(以像素為單位) - 正如所料。 但不推薦使用getWidth()和getHeight()方法。 相反,你可以使用:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
正如Martin Koubek所說。
這給你屏幕分辨率:
WindowManager wm = (WindowManager)context.getSystemService(context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point outSize = new Point();
display.getSize(outSize);
如果你的目標只是在屏幕上繪制一個橢圓形,那么考慮創建自己的自定義View
而不是使用AbsoluteLayout
。 您的自定義View
必須覆蓋onDraw(android.graphics.Canvas)
,當視圖呈現其內容時將調用它。
以下是一些非常簡單的示例代碼,可能有助於您入門:
public class MainActivity extends Activity {
private final Paint mPaint = new Paint();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
// create a nested custom view class that can draw an oval. if the
// "SampleView" is not specific to the Activity, put the class in
// a new file called "SampleView.java" and make the class public
// and non-static so that other Activities can use it.
private static class SampleView extends View {
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.CYAN);
// smoothen edges
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(4.5f);
// set alpha value (opacity)
mPaint.setAlpha(0x80);
// draw oval on canvas
canvas.drawOval(new RectF(50, 50, 20, 40), mPaint);
}
}
}
kabuko的答案是正確的,但可能會更清楚,所以讓我澄清一下。
getWidth()
和getHeight()
是(正確地)給你0,因為在你調用它們時它們沒有在布局中繪制。 嘗試在addView()
之后調用按鈕上的兩個方法(在繪制視圖並且在布局中出現之后)並查看是否為您提供了預期的結果。
有關更多信息,請參閱此帖子 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.