简体   繁体   English

Android根据屏幕尺寸缩放文本和图像(手机与平板电脑)

[英]Android scaling text and images based on screen size (phone vs tablet)

I have a good understanding of the difference between px, dp and sp. 我很好地理解了px,dp和sp之间的区别。 The problem is, when I put a TextView ( or ImageView ) on the screen, the dpi is irrelevant. 问题是,当我在屏幕上放置TextView(或ImageView)时,dpi是无关紧要的。 On a 3 inch high screen with 160 dpi, a text size of 80px will be 1/2 an inch, or 1/6th the height. 在具有160 dpi的3英寸高屏幕上,80px的文本大小将是1/2英寸,或高度的1/6。 On a 7 inch high screen with 160 dpi, it's still 1/2 inch, but that is now 1/14th of the height. 在具有160 dpi的7英寸高屏幕上,它仍然是1/2英寸,但现在是高度的1/14。

The problem is, using dp (or sp), what looks good on a phone disappears on a tablet. 问题是,使用dp(或sp),手机上看起来不错的东西会在平板电脑上消失。

Is there a way to specify fractional screen size, like percent? 有没有办法指定分数屏幕大小,如百分比?

I not sure if you can specify size in percent, but why you dont define different dimensions for screen in dimen.xml file. 我不确定您是否可以指定百分比大小,但为什么不在dimen.xml文件中为屏幕定义不同的维度。 It is very hard to use the same values dp and sp for all screen sizes. 对于所有屏幕尺寸,很难使用相同的值dp和sp。

You need to create different dimens.xml different resource folders 您需要创建不同的dimens.xml不同的资源文件夹

 res/values-ldpi/dimens.xml
 res/values-mdpi/dimens.xml
 res/values-hdpi/dimens.xml

Next add some values to these files 接下来为这些文件添加一些值

 <!-- in values-ldpi/dimens.xml -->
 <dimen name="textSize">25dip</dimen>

 <!-- in values-mdpi/dimens.xml -->
 <dimen name="textSize">20dip</dimen>

Finaly reference to dimensions in your layout file: 最终引用布局文件中的维度:

<TextView
    android:layout_toRightOf="@id/at"
    android:layout_below="@id/hw"
    android:textSize="@dimen/textSize"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

EDIT: 编辑:

If you want set height of image to 1/10 of screen size you need to use android:layout_weight attribute in your view containers. 如果要将图像的高度设置为屏幕大小的1/10,则需要在视图容器中使用android:layout_weight属性。 Check the example below. 请查看以下示例。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/imageView"
            android:background="@drawable/ic_launcher" />
    </LinearLayout>

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:weightSum="1"
            android:layout_weight="9"
            >
 </LinearLayout>

 </LinearLayout>

This will give textSize depending upon the density. 这将根据密度给出textSize。

  TextView text = new TextView(this);
  text.setText("text");
  TextView.setTextSize(16 * getResources().getDisplayMetrics().density);

OR 要么

Create dimens.xml in values-ldpi,mdpi and hdpi. 在values-ldpi,mdpi和hdpi中创建dimens.xml。

<dimen name="textSize">30dip</dimen>
<dimen name="textSize">10dip</dimen>

and then add this in your layout. 然后在您的布局中添加它。

<TextView
android:textSize="@dimen/textSize"
    //rest code here
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM