Android : why do I have a white padding on two sid

2019-06-14 15:28发布

I know that this kind of questions has already been asked elsewhere. But I don't understand why I have got a problem in the following specific case. Here is the very simple layout:

 <RelativeLayout
        android:id="@+id/headerLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#D0D0D0"
        android:orientation="horizontal"

        >


        <ImageView
            android:id="@+id/appImageView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"                
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@+id/appNameTextView"
            android:layout_alignTop="@+id/appNameTextView"
            android:scaleType="centerInside"
            android:src="@drawable/common_ic_googleplayservices"
            android:background="#FF0000"
            />


        <TextView
            android:id="@+id/appNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/appImageView"
            android:text="Here is some text"
            android:textColor="#000000"

            />

    </RelativeLayout>

Here is the result:

enter image description here

common_ic_googleplayservices is a square image which is scaled down. Why do I have a padding (see the red area) => my image is no more square.

How to solve the problem ? (please test quickly before giving an answer :) )

Thanks !!!

3条回答
贼婆χ
2楼-- · 2019-06-14 15:54

What you are trying to do is not directly possible with RelativeLayout or any other default layouts, for this you need PercentRelativeLayout. But I suggest not to include any additional dependencies for such Task.

Step-1: Make your UI like following

<RelativeLayout
    android:id="@+id/headerLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#D0D0D0" >

    <ImageView
        android:id="@+id/appImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:background="#FF0000"
        android:src="@drawable/common_ic_googleplayservices" />

    <TextView
        android:id="@+id/appNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/appImageView"
        android:paddingStart="10dp"
        android:text="Here is some text"
        android:layout_centerVertical="true"
        android:textColor="#000000" />

</RelativeLayout>

Step-2: Make following changes in JAVA code.

final ImageView imageView = (ImageView) findViewById(R.id.image_view);
    imageView.post(new Runnable() {
        @Override
        public void run() {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
            layoutParams.width = layoutParams.height;
            imageView.setLayoutParams(layoutParams);
        }
    });
查看更多
我命由我不由天
3楼-- · 2019-06-14 16:03

Use this

    android:scaleType="fitXY"
    android:adjustViewBounds="true"
查看更多
趁早两清
4楼-- · 2019-06-14 16:11

Try this,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignTop="@+id/tv"
        android:layout_alignBottom="@+id/tv"
        android:src="@drawable/common_ic_googleplayservices" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/iv"
        android:gravity="center_vertical"
        android:text="TextView"
        android:textSize="30dp"

        />

</RelativeLayout>
查看更多
登录 后发表回答