簡體   English   中英

如何為不同的屏幕尺寸android使用不同的圖像?

[英]How to use different images for different screen sizes android?

我已將不同尺寸的圖片放在 mdpi、hdpi、xhdpi 等中,但我使用該圖像的 ImageButtons 在每個設備上的大小似乎相同。

上次我這樣做時,它只將圖像放在文件夾中。 我不記得我到底做了什么。

我想在不同的屏幕上顯示不同大小的圖像。 我有

  • 5.7寸手機
  • 5.0寸手機
  • 7.0寸平板

我的應用程序在所有這些按鈕中顯示相同大小的圖像按鈕。

<?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>

注意:已經很長時間了,但事實證明將文件放在正確的文件夾中是有效的。 文件的名稱應該完全相同。 您可以通過將圖像與文件夾中已有的圖像進行比較來驗證圖像的大小。

您可以使用dimenify插件。 它將為不同的屏幕創建標准的尺寸文件夾集,並為這些屏幕轉換尺寸值。 這樣,您的按鈕將在每個屏幕上都有自己的大小:)

首先要解決的是“屏幕尺寸”的含義的混淆。 你的意思是物理尺寸(例如 4 英寸寬 vs 7 英寸寬)還是邏輯尺寸(例如 360dp 寬 vs 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

暫無
暫無

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

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