【问题标题】:how can make my xml images fit for all devices?如何使我的 xml 图像适合所有设备?
【发布时间】:2017-01-11 12:53:31
【问题描述】:

我是 android 设计的新手,所以我希望我的主屏幕图像适合所有设备我希望这个 MainScreen 页面适合所有设备。它有 3 张我给权重的图像,但如果我在标签上测试它似乎没有任何效果。它看起来不像我想要的那样,这是图像

这是我的 XML。我知道我不应该给 dp 高度,而是如何让它看起来像这样而不给出尺寸。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.zeba.broccoli.MainActivity" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginTop="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:weightSum="1">

            <ImageView
                android:id="@+id/ms1"
                android:layout_width="match_parent"
                android:layout_height="374dp"
                android:layout_weight="7"
                android:scaleType="fitXY"
                android:src="@drawable/avatar" />


        </LinearLayout>

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


            <ImageView
                android:id="@+id/ms2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"

                android:src="@drawable/ee"

                android:layout_weight=".5" />

            <ImageView
                android:id="@+id/ms3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@drawable/dd"
                android:layout_weight=".5" />
        </LinearLayout>
    </LinearLayout>


</RelativeLayout>

【问题讨论】:

  • 图像的固定尺寸可能因设备而异。使用 wrap_content 作为尺寸,并告诉你的设计师根据不同的屏幕分辨率设计图像。
  • 如果你不想给出尺寸,那么你需要将不同尺寸的图像放在单独的可绘制文件夹中,即drawable-xhdpi、drawable-xxhdpi等
  • 如果我根据 hdpi、xxhdpi 等设备调整图像大小..就像那样..thn 它可以适合吗?
  • 没关系,但是在这里我可以在不给出高度大小的情况下给我的图像完美的外观..例如,这里我给出了 374dp..我没有给出 shd
  • 我想用重量来建造..但是重量在这里不起作用

标签: android android-layout android-imageview


【解决方案1】:

使用此代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


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

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.7">

                <ImageView
                    android:id="@+id/ms1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@mipmap/ic_launcher" />


            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.3">


                <ImageView
                    android:id="@+id/ms2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight=".5"
                    android:scaleType="fitXY"
                    android:src="@mipmap/ic_launcher" />

                <ImageView
                    android:id="@+id/ms3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight=".5"
                    android:scaleType="fitXY"
                    android:src="@mipmap/ic_launcher" />

            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

希望对你有帮助

【讨论】:

  • 我已经用 ic_launcher 替换了您的图像资源。所以根据您的需要进行相应的放置。谢谢。
  • 但我需要在尺寸上做一些工作,因为对于某些设备,图像输出不合适..可能需要调整尺寸
  • 它会自动调整。只需使用更多设备进行测试。您会明白的。
【解决方案2】:

请参阅以下 xml 解决方案以获取您需要的内容。

您始终可以通过播放 layout_weight 参数来更改每个视图元素的相对宽度和高度。就像以百分比形式给出宽度/高度:

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

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@android:drawable/btn_default"
    android:scaleType="fitXY"
    android:layout_weight="70"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="30">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@android:drawable/btn_default"
        android:scaleType="fitXY"
        android:layout_weight="1"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@android:drawable/btn_default"
        android:scaleType="fitXY"
        android:layout_weight="1"/>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-08
    • 2011-03-19
    相关资源
    最近更新 更多