Android Studio模拟器中密码EditText控件不显示问题求助
我是Android Studio新手,正在开发一款简单的事件追踪应用。密码对应的EditText在设计视图中可见,但在模拟器运行应用时却不显示,完全不清楚原因。
试过这些操作都没用:
- 更换模拟器
- 移除CardView组件
- 从LinearLayout切换到RelativeLayout
这是我的第一个应用,还在学习各类组件的使用与操作,附上XML布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/custom_text" tools:context=".MainActivity"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:backgroundTint="#39423F3F"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/image" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="100dp" android:orientation="vertical" android:padding="24dp"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="0dp" android:background="@drawable/custom_text" android:drawablePadding="8dp" android:hint=" Username" android:outlineAmbientShadowColor="#A25252" android:textColor="@color/black" android:textColorHighlight="@color/cardview_dark_background" /> <EditText android:id="@+id/user_password" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="15dp" android:background="@drawable/custom_text" android:drawablePadding="8dp" android:hint=" Password" android:textColor="@color/black" android:textColorHighlight="@color/cardview_dark_background" /> <Button android:id="@+id/loginButton" android:layout_width="200dp" android:layout_height="60dp" android:layout_marginLeft="60dp" android:layout_marginTop="30dp" android:backgroundTint="@color/black" android:clickable="true" android:text="Login" android:textColor="@color/white" android:textSize="18sp" /> <Button android:id="@+id/signUpButton" android:layout_width="200dp" android:layout_height="60dp" android:layout_marginLeft="60dp" android:layout_marginTop="8dp" android:backgroundTint="@color/black" android:clickable="true" android:enabled="true" android:text="Sign Up" android:textColor="@color/white" android:textSize="18sp" /> </LinearLayout> </androidx.cardview.widget.CardView> </LinearLayout>
排查背景资源问题
密码EditText和外层LinearLayout用了同一个@drawable/custom_text背景,如果这个drawable的颜色和CardView的#39423F3F背景色接近或透明,就会导致EditText在模拟器中看起来“消失”。临时把密码EditText的背景改成@android:drawable/edit_text测试,或者修改custom_text的颜色参数。解决组件遮挡问题
CardView是FrameLayout子类,内部的ImageView和LinearLayout是平级的。如果ImageView的图片尺寸过大,会覆盖下方的LinearLayout(包括密码EditText)。给ImageView加android:layout_gravity="top|center_horizontal",或者限制它的android:maxWidth和android:maxHeight,避免遮挡。调整布局适配性
LinearLayout设置了android:layout_marginTop="100dp",在小屏模拟器上可能把密码EditText挤到屏幕外。换成百分比值android:layout_marginTop="20%",或者改用ConstraintLayout做自适应布局,也可以给CardView加android:padding="16dp"防止内容被截断。清理缓存重建项目
Android Studio的缓存有时会导致布局渲染异常,执行Build > Clean Project和Build > Rebuild Project,重启模拟器再试。
内容的提问来源于stack exchange,提问作者SO8787

