[英]Android: Width of a grid of buttons screen does not change with different screen resolutions
我的應用程序制作了一個16x16的按鈕網格。 這些寬度和高度均設置為30dp。 這就是480x480dp。 在800x480屏幕上測試時,應用顯示正常。 但是,當我將模擬器設置為400x240時,該應用程序確實很難看,並且按鈕的網格比屏幕更寬。
為了解決這個問題,我將按鈕的寬度和高度設置為一個名為“ knopAfmeting”的變量。 該變量看起來像是用以下代碼設置的:
Display display = getWindowManager().getDefaultDisplay();
int schermBreedte = display.getWidth();
int knopAfmeting = schermBreedte/16;
然后將按鈕設置如下:
Button button = new Button (this);
button.setHeight(knopAfmeting);
button.setWidth(knopAfmeting);
但是,當在400x240px屏幕上進行測試時,該應用仍會顯示錯誤的按鈕網格。 我究竟做錯了什么?
試試這個...您不需要為按鈕設置高度/寬度,它們將相對設置。
<?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"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
<Button
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="0dp" />
</LinearLayout>
</LinearLayout>
========第二個建議========
抱歉,我以錯誤的方式理解了您的問題..無論如何,我檢查了您的代碼..您總是基於寬度計算knopAfmeting的錯誤,如果height等於或大於width是正確的。您需要根據高度來計算knopAfmeting ..換句話說,獲得較小的尺寸。
所以
Display display = getWindowManager().getDefaultDisplay();
int schermBreedte =0;
if(display.getWidth()>display.getHeight())
schermBreedte=display.getHeight()
else
schermBreedte=display.getWidth();
int knopAfmeting = schermBreedte/16;
只是您會在右側獲得一些空間..您可以將它們全部居中以使其更好..
祝好運 ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.