简体   繁体   中英

how can make my xml images fit for all devices?

I'm new to designing of android, So I want my main screen images fit to all devices i want this MainScreen page fit to be in all devices. It has 3 images I have given weight but nothing seems to work if I test it on tab. Its not looking the way I want, here is the image

在此处输入图片说明

here is my XML .I know I should not give dp for height but how to make it look like that without giving sizes.

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

Use this code:

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

Hope this will help you

See following xml solution to get what you need.

You always can change relative width and height of the each view element by playing layout_weight parameter. Its like give width/height in percents :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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