![](/img/trans.png)
[英]Issue with layout in android studio. How do I get my relative layout to cover the entire screen and not stay stuck in the top left?
[英]how do i accurately calculate my layout (button) height relative to screen size?
大家好,我目前正在使用我的第一個應用程序,但遇到了一個奇怪的問題。 我的布局中有5個按鈕,它們的高度由一個函數計算得出,以便為每個屏幕獲取正確的尺寸。
該功能運行良好,幾乎可以正確設置每個按鈕的高度,除了底部的一小行外,我想這是某種舍入誤差嗎?
也許有人可以指出我正確的方向,但我無法弄清楚。 提前致謝。
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
int minusthis = 0;
Context context = this;
int resource = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resource > 0) {
minusthis = context.getResources().getDimensionPixelSize(resource);
}
int screenHeight = displayMetrics.heightPixels - minusthis;
int tilesize = (screenHeight / 5);
如您所見,我得到displayMetrics並將它們除以5,因為我有5個圖塊,它們必須恰好是屏幕的1/5。 稍后,“ tilesize”將應用於代碼中的按鈕。
在我拍的照片中,您可以看到按鈕幾乎完全在屏幕上伸展了,除了我用紅色圈出的細白線。 這條線似乎很小,但我無法使其消失,這確實讓我感到煩惱,在其他活動中,如果我使用不同大小的按鈕和面板,這種現象也會更加明顯。
因此,如果您已經閱讀了本文,那么已經謝謝您了! 如果您發現任何改進我的代碼的方法,請告訴我:)
與其動態地計算尺寸,不如使用Linear Layout和具有weight屬性的子Text View 。 它將解決您的問題。
這是示例代碼
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:text="item1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:gravity="center"
android:text="item2"
/>
</LinearLayout>
我們有更好的選擇來顯示相同重量的視圖。 您可以將LinearLayout與android:orientation="vertical"
。 將所有TextView添加到LinearLayout中並設置All TextView屬性android:layout_width="0dp"
, android:layout_height="match_parent"
和android:layout_weight="1"
。
使用LinearLayout和weight屬性,我們可以輕松地平均划分屏幕。
與其嘗試動態獲取尺寸,不如嘗試線性布局並給出高度和重量
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:text="item1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/holo_blue_light"
android:gravity="center"
android:text="item2"
/>
如果您想水平添加按鈕而不是使用方向水平和weightsum屬性
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightsum="10"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:text="item1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="5"
android:background="@android:color/holo_blue_light"
android:gravity="center"
android:text="item2"
/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.