【问题标题】:Using resolution independant fonts within an android layout在 android 布局中使用与分辨率无关的字体
【发布时间】:2014-06-22 01:49:44
【问题描述】:

背景

我正在为一个 android 应用程序创建一个布局,并且到目前为止一直使用以sp(或dp)表示的字体大小,以便它们可以针对不同的设备正确缩放。然而结果与我所希望的不同。结果是文本在所有设备上都是相同的物理大小(以英寸为单位),而我希望它占据屏幕的相同比例。

目前,我的菜单按钮在平板电脑上看起来有点小,但在手机上却大得离谱(请参阅屏幕截图;虽然它并不能说明手机版本看起来有多糟糕)。

使用px 实际上为我提供了我想要的这些特定设备的结果,但那是因为它们具有相似的分辨率(但像素密度非常不同)。如果我根据我希望它在这两个设备上的外观(均约为 1,200 x 700)使用像素,并且 Galaxy S10 的物理尺寸与 Galaxy S3 相同,但分辨率为 10,000 x 7,000,那么我的按钮看起来会小得离谱。

问题

如何设置与分辨率无关(与密度无关)的字体大小(或任何大小)?

Galaxy S3 - 大号手机

标签 3 - 中等平板电脑

当前布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/menubackground"
    >
    
   <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical" 
        android:layout_gravity="center"
        android:gravity="center"
     >
         <Button
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:text="@string/singleplayer"
         android:layout_marginTop="50dp"
         android:layout_marginBottom="50dp"
         android:textSize="50sp"
         android:onClick="singleplayer" 

         />
          
         <Button
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:text="@string/twoplayer"
         android:layout_marginTop="50dp"
         android:layout_marginBottom="50dp"
         android:textSize="50sp"
         android:onClick="twoplayer" 
         /> 
         
         <Button
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:text="@string/campaign"
         android:layout_marginTop="50dp"
         android:layout_marginBottom="50dp"
         android:textSize="50sp"
         android:onClick="campaign" 
         />
         
         <Button
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"
         android:text="@string/ai_vs_ai"
         android:layout_marginTop="50dp"
         android:layout_marginBottom="50dp"
         android:textSize="50sp"
         android:onClick="aiVsAI" 
         />
    </LinearLayout>
      

</LinearLayout>

【问题讨论】:

    标签: java android resolution


    【解决方案1】:

    首先,正如您所说,dp(与密度无关的像素)应该在所有设备中看起来大致相同的“物理”大小,无论显示密度如何,而 sp(缩放像素)应该根据用户偏好进行缩放.因此它们不适合您尝试做的事情。

    如果您不想要不同的布局,而只想要不同的字体大小,您可以为这些属性使用对资源的引用。例如,您可以为 valuesvalues-large 创建替代 xml 文件:

    values:

    <dimen name="mainMenuOption">50sp</dimen>
    

    values-large:

    <dimen name="mainMenuOption">80sp</dimen>
    

    然后,在你的布局中,使用:

    <Button
        android:layout_height="wrap_content"
        android:textSize="@dimen/mainMenuOption"
        ...
    

    大多数(尽管不是全部)属性都可以使用对资源的引用。

    当然,这并不能实现“分辨率独立”,但它允许根据设备特性自定义“小部分”而不是整个布局。

    【讨论】:

    • 现在这个更像了。避免复制粘贴代码重用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多