android:src="@drawable/warning"

本文详细解析了Android中ImageView组件的src与background属性的区别,包括它们如何影响图片展示及scaleType的应用。同时介绍了不同scaleType的具体含义及其应用场景。

ImageView中XML属性src和background的区别:

background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸。src是图片内容(前景),bg是背景,可以同时使用。

此外:scaleType只对src起作用;bg可设置透明度,比如在ImageButton中就可以用android:scaleType控制图片的缩放方式,示例代码如下:

   <ImageView android:id="@+id/img" 
    android:src="@drawable/logo"
    android:scaleType="centerInside"
    android:layout_width="60dip"
    android:layout_height="60dip"
    android:layout_centerVertical="true"/>

  说明:centerInside表示按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度。

  注意:控制的图片为资源而不是背景,即android:src="@drawable/logo",而非android:background="@drawable/logo"。程序中动态加载图片也类似,如:应该imgView.setImageResource(R.drawable.*);而非imgView.setBackgroundResource(R.drawable.*);


附:更详细的scaleType说明:

CENTER /center 在视图中心显示图片,并且不缩放图片

CENTER_CROP / centerCrop 按比例缩放图片,使得图片长 (宽)的大于等于视图的相应维度

CENTER_INSIDE / centerInside 按比例缩放图片,使得图片长 (宽)的小于等于视图的相应维度

FIT_CENTER / fitCenter 按比例缩放图片到视图的最小边,居中显示

FIT_END / fitEnd 按比例缩放图片到视图的最小边,显示在视图的下部分位置

FIT_START / fitStart 把图片按比例扩大/缩小到视图的最小边,显示在视图的上部分位置

FIT_XY / fitXY 把图片不按比例缩放到视图的大小显示

MATRIX / matrix 用矩阵来绘制


二者同时存在的时候,以src的设置进行显示

提示我:@layout/ item_widget includes views not allowed in a RemoteView, 但我的item_widget中并未发现此类控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll_site" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:gravity="center_horizontal" android:orientation="vertical"> <FrameLayout android:layout_width="@dimen/tpds_all_dp_64" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_site" android:layout_width="match_parent" android:layout_height="wrap_content" tools:src="@drawable/icon_sdn_home_site" /> <LinearLayout android:id="@+id/alert_icon" android:layout_width="wrap_content" android:layout_height="@dimen/tpds_all_dp_16" android:layout_gravity="top|end" android:layout_marginStart="-10dp" android:layout_marginTop="@dimen/tpds_all_dp_1" android:background="@drawable/circular_corner_red_8dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/tpds_all_dp_3" android:src="@drawable/icon_warning_mini" /> <TextView android:id="@+id/alert_num" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="@dimen/tpds_all_dp_5" android:background="@drawable/circular_corner_red_8dp" android:gravity="start" android:textColor="@color/color_white100" android:textSize="@dimen/text_size_12" tools:text="1" /> </LinearLayout> </FrameLayout> <LinearLayout android:id="@+id/ll_num" android:layout_width="wrap_content" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/view_space" android:layout_width="16dp" android:layout_height="16dp" /> <TextView android:id="@+id/tv_online" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/text_size_14" tools:text="N" tools:textColor="@color/omada_color_green" /> <TextView android:id="@+id/div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:textColor="?android:textColorPrimary" android:textSize="@dimen/text_size_14" /> <TextView android:id="@+id/tv_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="?android:textColorPrimary" android:textSize="@dimen/text_size_14" tools:text="A" /> <ImageView android:id="@+id/alert_attention" android:layout_width="16dp" android:layout_height="16dp" android:layout_marginStart="6dp" android:src="@drawable/icon_info_yellow" /> </LinearLayout> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/global_sites" android:textColor="@color/color_text_secondary_1" android:textSize="@dimen/text_size_12" /> </LinearLayout>
10-13
<?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" android:background="#E6E6E6" tools:context=".MainActivity"> <ImageView android:id="@+id/iv" android:layout_width="70dp" android:layout_height="70dp" android:layout_gravity="center" android:layout_marginTop="30dp" android:background="@mipmap/head" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="horizontal" android:layout_gravity="center" android:layout_marginRight="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="30dp"> <TextView android:id="@+id/tv_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" 账号:" android:textSize="25sp" android:textStyle="bold"/> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/tv_1" android:background="@null" android:maxLines="1"/> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="horizontal" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:layout_marginLeft="10dp"> <TextView android:id="@+id/tv_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" 密码:" android:textSize="25sp" android:textStyle="bold" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/tv_2" android:inputType="textPassword" android:background="@null" android:maxLines="1"/> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_marginHorizontal="60dp"> <Button android:id="@+id/signup" android:layout_width="80dp" android:layout_height="50dp" android:background="#3C8DC4" android:text="注册" android:textColor="#ffffff" /> <Button android:id="@+id/login" android:layout_width="80dp" android:layout_height="50dp" android:background="#3C8DC4" android:layout_toRightOf="@+id/signup" android:layout_marginLeft="50dp" android:text="登录" android:textColor="#ffffff" /> </RelativeLayout> </LinearLayout><?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#80000000"> <com.google.android.material.card.MaterialCardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="24dp" app:cardCornerRadius="16dp" app:cardElevation="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="24dp"> <!-- 标题栏 --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户注册" android:textSize="20sp" android:textStyle="bold" android:layout_centerHorizontal="true" /> <ImageButton android:id="@+id/closeButton" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentEnd="true" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/ic_close" android:contentDescription="关闭" /> </RelativeLayout> <!-- 用户名输入 --> <com.google.android.material.textfield.TextInputLayout android:id="@+id/registerUsernameLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名" app:startIconDrawable="@drawable/ic_person" android:layout_marginBottom="16dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/registerUsernameEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textCapWords" android:imeOptions="actionNext" /> </com.google.android.material.textfield.TextInputLayout> <!-- 邮箱输入 --> <com.google.android.material.textfield.TextInputLayout android:id="@+id/registerEmailLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="邮箱地址" app:startIconDrawable="@drawable/ic_email" android:layout_marginBottom="16dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/registerEmailEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:imeOptions="actionNext" /> </com.google.android.material.textfield.TextInputLayout> <!-- 密码输入 --> <com.google.android.material.textfield.TextInputLayout android:id="@+id/registerPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" app:startIconDrawable="@drawable/ic_lock" app:endIconMode="password_toggle" android:layout_marginBottom="16dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/registerPasswordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:imeOptions="actionNext" /> </com.google.android.material.textfield.TextInputLayout> <!-- 确认密码输入 --> <com.google.android.material.textfield.TextInputLayout android:id="@+id/registerConfirmPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="确认密码" app:startIconDrawable="@drawable/ic_lock" app:endIconMode="password_toggle" android:layout_marginBottom="24dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/registerConfirmPasswordEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:imeOptions="actionDone" /> </com.google.android.material.textfield.TextInputLayout> <!-- 注册按钮 --> <com.google.android.material.button.MaterialButton android:id="@+id/registerButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" android:textAllCaps="false" app:icon="@drawable/ic_person_add" app:iconGravity="textStart" style="@style/Widget.Material3.Button.ElevatedButton" /> <!-- 底部提示 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册即表示您同意我们的服务条款和隐私政策" android:textSize="12sp" android:gravity="center" android:layout_marginTop="16dp" android:textColor="@color/gray" /> </LinearLayout> </com.google.android.material.card.MaterialCardView> </androidx.coordinatorlayout.widget.CoordinatorLayout>package com.example.login; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button Bt_login; private Button BT_signup; private EditText Username; private EditText Password; static String username1; static String password1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bt_login = (Button) findViewById(R.id.login); BT_signup = (Button) findViewById(R.id.signup); Username = (EditText) findViewById(R.id.username); Password = (EditText) findViewById(R.id.password); Onclick onclick = new Onclick(); Bt_login.setOnClickListener(onclick); BT_signup.setOnClickListener(onclick); } class Onclick implements View.OnClickListener { @Override public void onClick(View v) { int id = v.getId(); if (id == R.id.login) { String username = Username.getText().toString(); String password = Password.getText().toString(); if (password.equals(password1) && username.equals(username1)) { Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); intent.setData(Uri.parse("https://www.baidu.com/")); startActivity(intent); } else { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("warning") .setMessage("您输入错误,请重新输入 您也有可能未注册") .setPositiveButton("重新登录", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(MainActivity.this, MainActivity.class); startActivity(intent); } }).setNegativeButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "正在退出", Toast.LENGTH_LONG).show(); } }).show(); } } else if (id == R.id.signup) { showRegisterDialog(); } else { throw new IllegalStateException("Unexpected value: " + v.getId()); } } } private void showRegisterDialog() { // 创建自定义对话框 AlertDialog.Builder builder = new AlertDialog.Builder(this); // 获取布局填充器 LayoutInflater inflater = getLayoutInflater(); View dialogView = inflater.inflate(R.layout.popupwindow_register, null); // 获取对话框中的控件 EditText registerUsername = dialogView.findViewById(R.id.registerUsernameEditText); EditText registerEmail = dialogView.findViewById(R.id.registerEmailEditText); EditText registerPassword = dialogView.findViewById(R.id.registerPasswordEditText); EditText registerConfirmPassword = dialogView.findViewById(R.id.registerConfirmPasswordEditText); Button registerButton = dialogView.findViewById(R.id.registerButton); ImageButton closeButton = dialogView.findViewById(R.id.closeButton); // 创建对话框 AlertDialog dialog = builder.setView(dialogView).create(); // 设置关闭按钮点击事件 closeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); // 设置注册按钮点击事件 registerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = registerUsername.getText().toString(); String email = registerEmail.getText().toString(); String password = registerPassword.getText().toString(); String confirmPassword = registerConfirmPassword.getText().toString(); // 输入验证 if (username.isEmpty()) { Toast.makeText(MainActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show(); return; } if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { Toast.makeText(MainActivity.this, "请输入有效的邮箱地址", Toast.LENGTH_SHORT).show(); return; } if (password.isEmpty() || password.length() < 6) { Toast.makeText(MainActivity.this, "密码长度至少6位", Toast.LENGTH_SHORT).show(); return; } if (!password.equals(confirmPassword)) { Toast.makeText(MainActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show(); return; } // 注册成功 username1 = username; password1 = password; // 同时更新主界面的用户名和密码输入框 Username.setText(username); Password.setText(password); Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); // 显示对话框 dialog.show(); // 设置对话框宽度匹配父布局 if (dialog.getWindow() != null) { dialog.getWindow().setLayout(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); } } } ERROR: AAPT: error: resource style/Theme.Material3.Light.DarkActionBar (aka com.example.login:style/Theme.Material3.Light.DarkActionBar) not found. error: failed linking references.
09-27
10-29
> Task :app:processDebugMainManifest FAILED [com.android.support:animated-vector-drawable:28.0.0] C:\Users\Administrator\.gradle\caches\transforms-3\3648fb114e268826532fb9efd04f940b\transformed\animated-vector-drawable-28.0.0\AndroidManifest.xml Warning: Namespace 'android.support.graphics.drawable' used in: com.android.support:animated-vector-drawable:28.0.0, com.android.support:support-vector-drawable:28.0.0. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:41:5-75 Warning: Element uses-permission#android.permission.INSTALL_PACKAGES at AndroidManifest.xml:41:5-75 duplicated with element declared at AndroidManifest.xml:10:5-74 F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:47:5-76 Warning: Element uses-permission#android.permission.CHANGE_WIFI_STATE at AndroidManifest.xml:47:5-76 duplicated with element declared at AndroidManifest.xml:35:5-76 F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:61:5-67 Warning: Element uses-permission#android.permission.INTERNET at AndroidManifest.xml:61:5-67 duplicated with element declared at AndroidManifest.xml:31:5-67 F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:64:5-75 Warning: Element uses-permission#android.permission.READ_PHONE_STATE at AndroidManifest.xml:64:5-75 duplicated with element declared at AndroidManifest.xml:19:5-75 F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:65:5-69 Warning: Element uses-permission#android.permission.CALL_PHONE at AndroidManifest.xml:65:5-69 duplicated with element declared at AndroidManifest.xml:22:5-69 F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:91:9-105:20 Error: android:exported needs to be explicitly specified for element <activity#com.dosen.watchtest.MainActivity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:307:9-320:20 Error: android:exported needs to be explicitly specified for element <activity#com.dosen.watchtest.activity.PhotoActivity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:119:9-123:19 Error: android:exported needs to be explicitly specified for element <service#com.dosen.watchtest.alarm.AlarmKlaxon>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:111:9-117:20 Error: android:exported needs to be explicitly specified for element <receiver#com.dosen.watchtest.alarm.AlarmReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:125:9-133:20 Error: android:exported needs to be explicitly specified for element <receiver#com.dosen.watchtest.alarm.AlarmInitReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:148:9-152:20 Error: android:exported needs to be explicitly specified for element <receiver#com.dosen.watchtest.receiver.WakeLockBroadCast>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:154:9-158:20 Error: android:exported needs to be explicitly specified for element <receiver#com.dosen.watchtest.receiver.CloudOpenReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:160:9-164:20 Error: android:exported needs to be explicitly specified for element <receiver#com.dosen.watchtest.receiver.SmsReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:71:9-76:20 Error: android:exported needs to be explicitly specified for element <receiver#io.rong.callkit.VoIPBroadcastReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:80:9-84:20 Error: android:exported needs to be explicitly specified for element <receiver#io.rong.callkit.util.RTCPhoneStateReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. F:\git13\WatchTest1\app\src\main\AndroidManifest.xml:46:9-61:20 Error: android:exported needs to be explicitly specified for element <receiver#io.rong.push.rongpush.PushReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. 是什么问题
06-12
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值