解决android线性布局中引入图片资源后修改图片大小的方法

在做登录界面的时候,在EditView中引用图片后,图片过大导致显示异常难看。
在这里插入图片描述如何将这个图片调整和右边的搜索框一样大小呢?
以下是他的XML文件代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="QQ号/手机号/微信号"
        android:paddingBottom="10dp"
        android:drawableLeft="@mipmap/name"
        />
    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:drawableLeft="@mipmap/key"
        android:paddingBottom="10dp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textColor="#FFFFFF"
        android:background="#FF009688"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="遇到问题? |  忘记密码"
        android:textColor="@color/black"
        android:layout_marginTop="15dp"
        android:layout_gravity="center_horizontal"

        />
</LinearLayout>

要想修改这里的图片大小必须使用java代码去修改
代码如下

package com.example.login;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText edit1=findViewById(R.id.edit1);//帐号图片
        EditText edit2=findViewById(R.id.edit2);//密码图片
        changeDraw(edit1,R.mipmap.name);
        changeDraw(edit2,R.mipmap.key);

    }

    private void changeDraw(EditText edit, int key) {
        Drawable drawable=getResources().getDrawable(key);
        drawable.setBounds(5,10,60,60);//第一个参数是左边距,第二个参数是上边距,第三个和第四个分别是图片长和宽
        edit.setCompoundDrawables(drawable,null,null,null);//将图片放在左边
    }
}

此时再看效果图
在这里插入图片描述

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值