[英]Setting height, and width of a relative layout in android
這是我的情況。
我有一個按照分辨率320 * 480制作的圖像(假設我正在使用mdpi mobile)。 我將此圖像作為背景添加到相對布局中。
現在,我的屏幕具有三個主要內容。
1- Title bar (The standard title bar of android)
2- My relative layout and its sub view image relative layout which is matched parent.
3- My menu bar at the bottom.
現在,由於我的圖像在菜單欄和標題欄之間伸展了。 我想使其適合屏幕。 下面是我正在使用的代碼。
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout()
{
if (notCreated == true)
{
notCreated = false;
}
else
{
mnHeight = mainLayout.getHeight();
Rect rect = getRawCoordinatesRect(mainLayout);
h = getWindowManager().getDefaultDisplay().getHeight() -
- rect.top - mainLayout.getHeight();
// rect.top to remove android standard title bar
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(image.getWidth(),LayoutParams.MATCH_PARENT);
params.topMargin = -1 * rect.top;
params.bottomMargin = -1 * h;
image.setLayoutParams(params);
}
}
});
現在這不起作用。 一點幫助將不勝感激。
注意:實際上,我只想將圖像的相對布局拉伸到320 * 480。 現在,它在標題欄和我的菜單欄之間搖擺不定。 例如,現在其尺寸為320 * 400
不知道這是否會幫助,但嘗試玩setScaleType()
嘗試不同的選擇。
image.setScaleType(ScaleType.FIT_CENTER);
我必須說老實話,說我不確定您要在這里做什么。 但是我將假定您要使圖像作為背景使用。如果這就是您想要的,則可以通過在主布局中設置android:background =“ @ drawable / your_image”屬性來簡單地做到這一點。您的布局文件。
如果您想要刪除標題欄,則可以通過將以下行添加到活動類中的onCreate方法中來完成此操作:
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
如果要刪除通知欄,則也可以在活動類的onCreate方法中完成此操作:
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
我希望這可以為您提供幫助:-)如果不是您要找的東西,請不要猶豫,重新制定您想要的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.