簡體   English   中英

使字體大小/邊距大小根據屏幕大小縮小?

[英]Making font size / margin size shrink according to screen size?

我的應用程序在較大的設備上看起來不錯,但在較小的設備上底部部分被切斷,因為按鈕不會根據屏幕尺寸調整大小。

如何讓它們根據屏幕尺寸調整大小,以便它們在所有尺寸上看起來都很好?

我在底部有一堆按鈕,這是按鈕的代碼:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:ads="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.uzerjamal.cricketscoremanager.MainActivity" android:background="#E9E9E9" android:orientation="vertical">
 <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
  <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.05" android:orientation="horizontal">
    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical">
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
    </LinearLayout>
    <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#9E9E9E" android:alpha="0.50" />
    <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical">
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
        <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="BUTTONeeee" android:layout_marginTop="0dp" android:layout_marginRight="4dp" android:layout_marginLeft="4dp" android:layout_marginBottom="4dp" android:textColor="@android:color/black" android:alpha="0.87" android:backgroundTint="#FF9800" />
  </LinearLayout>
 </LinearLayout>
</LinearLayout>

嘗試sdp 庫

1.在你的 app/build.gradle 添加如下依賴

dependencies {
  compile 'com.intuit.sdp:sdp-android:1.0.4'
}

2.用sdp值替換dp

例如,如果您的寬度為 10dp,例如

 android_layout:width="10dp"

替換為

android_layout:width="@dimen/_10sdp" 

如果你想使用它的java類

Resources res = context.getResources();
float widthInSdp = res.getDimension(R.dimen._10sdp);

int widthInPixels = (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_PX, widthInSdp, res.getDisplayMetrics());

注意:10sdp 在不同的設備分辨率上

10sdp on 330dp device = 12dp
10sdp on 420dp device = 14dp
10sdp on 480dp device = 16dp
10sdp on 540dp device = 18dp

相應地使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM