[英]Android layout: distinguishing an 800x480 large device from a 1024x600 tablet
[英]Different screen resolution showing good on tablet (1024x768), but bad on phone(800x480)
我的Android應用程序有問題。 當我使用分辨率為1024x768的hp觸摸板時,一切正常。 但是,當我在800x480(Desire HD)上進行測試時,發生了這種情況:
這是我的XML文件:
<?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:id="@+id/linearLayoutH1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:orientation="vertical"
android:weightSum="1" >
<EditText
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:layout_weight=".50"
/>
<EditText
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:layout_weight=".50"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutH3"
android:layout_width="fill_parent"
android:layout_height="100sp"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="@+id/buttonClear"
style="@style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/custombuttonred"
android:text="@string/clear"
android:layout_weight=".40"
android:textSize="50sp" />
<ImageButton
style="@style/ButtonText"
android:id="@+id/c101_image"
android:layout_width="40sp"
android:layout_height="100sp"
android:scaleType="centerInside"
android:background="@drawable/cloud101"
android:layout_weight=".20" />
<Button
android:id="@+id/buttonBackspace"
style="@style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/custombuttonred"
android:text="@string/backspace"
android:layout_weight=".40"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutH2"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:layout_weight=".25"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="@+id/button1"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="@string/b1"
android:textSize="110sp" />
<Button
android:id="@+id/button2"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="2"
android:textSize="110sp" />
<Button
android:id="@+id/button3"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="3"
android:textSize="110sp" />
<Button
android:id="@+id/buttonDevide"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombuttonblue"
android:text="/"
android:textSize="110sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutH3"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:layout_weight=".25"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="@+id/button4"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="4"
android:textSize="110sp" />
<Button
android:id="@+id/button5"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="5"
android:textSize="110sp" />
<Button
android:id="@+id/button6"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="6"
android:textSize="110sp" />
<Button
android:id="@+id/buttonMult"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombuttonblue"
android:text="X"
android:textSize="110sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutH4"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:layout_weight=".25"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="@+id/button7"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="7"
android:textSize="110sp" />
<Button
android:id="@+id/button8"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="8"
android:textSize="110sp" />
<Button
android:id="@+id/button9"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="9"
android:textSize="110sp" />
<Button
android:id="@+id/buttonMinus"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombuttonblue"
android:text="-"
android:textSize="110sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutH5"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:layout_weight=".25"
android:orientation="horizontal"
android:weightSum="1.0" >
<Button
android:id="@+id/button0"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombutton"
android:text="0"
android:textSize="110sp" />
<Button
android:id="@+id/buttonDot"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombuttonblue"
android:text="."
android:textSize="110sp" />
<Button
android:id="@+id/buttonEq"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombuttongreen"
android:text="="
android:textSize="110sp" />
<Button
android:id="@+id/buttonPlus"
style="@style/ButtonText"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_weight=".25"
android:background="@drawable/custombuttonblue"
android:text="+"
android:textSize="110sp" />
</LinearLayout>
</LinearLayout>
我已經讀過關於不同屏幕尺寸的問題,但是無論我使用sp還是dp,在800x480屏幕上看起來還是很糟糕。 我應該怎么做才能使字體正確顯示並且圖片居中並不會太緊?
第一:您不應該使用sp
作為視圖尺寸。 使用dp
表示視圖的尺寸,使用sp
表示文本的大小。 第二:如果使用weightsum
和layout_weight
,則視圖寬度不需要尺寸,則給它們提供0dp值,它們將被繪制為正常。
例如,如果您使用此配置:
<ImageButton
style="@style/ButtonText"
android:id="@+id/c101_image"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:scaleType="centerInside"
android:background="@drawable/cloud101"
android:layout_weight=".20" />
您的imageButton應該在所有屏幕上正確顯示。 對所有視圖使用相同的技術,它應該可以解決您的問題。
注意 :為您的文本使用較小的字體:)
祝你好運,阿克德
您的textSize對於該設備而言太大。
在res目錄中創建一個常規布局的文件夾,然后在其中粘貼布局的副本。 將按鈕上的110sp更改為較小的值。 也許開始嘗試使用70sp之類的東西。 測試不同大小,直到看起來不錯為止。
另外,如果您打算讓應用程序支持小屏幕,則還需要為其制作布局文件的另一個副本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.