如何在不同的屏幕尺寸android上使用不同的图像?

时间:2017-09-10 15:28:27

标签: android imageview android-resources

我把不同尺寸的照片放在mdpi,hdpi,xhdpi e.t.c.但是,使用该图像的My ImageButtons在每台设备上看起来大小相同。

所以我需要在清单文件中添加一些东西? 或者我需要在Java代码中做些什么。上次我这样做是通过只将图像放在文件夹中。我不记得我到底做了什么。

我想在不同的屏幕上显示不同大小的图像。我有一个5.7英寸手机,一个5.0英寸手机和一个7英寸平板电脑。我的应用程序在所有这些按钮中显示相同大小的图像按钮。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg"
tools:context="test.music.MainActivity">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dip">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nowplayingname_txtv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="1"
            android:scrollbars="horizontal"
            android:text="Ek Mulaqat-www.songsfarm.ifo"
            android:textColor="@color/title"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/npduration_txtv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:maxLines="1"
            android:text="05:12"
            android:textColor="@android:color/darker_gray" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/settings_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        android:paddingLeft="20dip"
        android:paddingRight="20dip"
        app:srcCompat="@mipmap/menu" />

</LinearLayout>


<ListView
    android:id="@+id/songs_listv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_weight="1"
    android:longClickable="true"
    android:layout_below="@+id/linearLayout3"
    android:layout_above="@+id/song_seekbar" />

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:orientation="horizontal"
    android:paddingBottom="16dp">


    <ImageButton
        android:id="@+id/prev_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        app:srcCompat="@mipmap/prev" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/pp_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        app:srcCompat="@mipmap/play" />

    <Space
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/next_imgbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:backgroundTint="@color/bg"
        app:srcCompat="@mipmap/next" />


</LinearLayout>

<SeekBar
    android:id="@+id/song_seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingBottom="16dip"
    android:paddingTop="16dip" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/linearLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/linearLayout3">


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#D3D3D3" />

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignTop="@+id/song_seekbar"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">


    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#D3D3D3" />

</LinearLayout>


</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您可以使用dimenify插件。它将为不同的屏幕创建标准的文件夹集,并为这些屏幕转换尺寸值。这样你的按钮就会有每个屏幕自己的大小:)

答案 1 :(得分:1)

首先要解决的问题是屏幕大小&#34;是什么意思的混淆。你的意思是物理尺寸(例如4英寸宽对7英寸宽)或者你的意思是逻辑尺寸(例如360dp宽对600dp宽)?

根据我的经验,很多开发人员都在关注物理尺寸,而没有意识到他们不应该这样做。显示的逻辑大小是您的应用向用户显示的更大因素。

如果您想为不同的物理尺寸使用不同的图像,那么使用资源框架就没有简单的方法。您必须使用DisplayMetrics对象的widthPixelsxdpi字段在Java中自己计算屏幕的物理大小,然后编写使用例如{ R.drawable.my_drawable_four_inchesR.drawable.my_drawable_seven_inches

如果要为不同的逻辑大小使用不同的图像,资源框架将对您有所帮助。您可以像这样设置可绘制资源:

res/
    drawable/
        my_drawable.png
    drawable-sw600dp/
        my_drawable.png

现在,在您的布局和Java代码中,您只需引用@drawable/my_drawableR.drawable.my_drawable,系统就会自动为您选择合适的代码。 600dp或更宽的设备将获得第二个文件,任何559dp或更窄的设备将获得第一个。

除此之外,您可能还需要考虑为不同的屏幕分辨率提供同一图像的多个副本。这有点超出了这个答案的范围,但你可以在这里阅读:https://developer.android.com/guide/practices/screens_support.html#density-independence

相关问题