Android布局中EditText与TextView无法显示的问题求助
Hey there! I see you're new to Android dev and Stack Overflow—welcome! Let's get your layout working properly so those missing views show up when you run the app. Here are the key issues and fixes:
1. You're using tools: attributes for runtime layout (they only work in the editor!)
The tools:layout_editor_absoluteX/Y attributes are only for previewing in Android Studio's layout editor—they don't affect how the layout renders when you run the app. For ConstraintLayout, you need to use real constraint attributes (app:layout_constraint...) to position your views correctly.
2. Syntax errors in your XML code
You've got mismatched double quotes (like android:id=""@+id/pwText"" instead of android:id="@+id/pwText") and unclosed strings (like the first EditText's android:hint), which can cause the layout parser to fail silently, hiding your views.
3. Garbled text in hints/buttons
The text in your hint and text attributes looks like garbled characters—make sure your XML file is saved with UTF-8 encoding, and type in the actual Chinese text you want (e.g., "请输入账号" instead of "鞎勳澊霐�").
Fixed Layout Code
Here's the corrected version of your layout with proper constraints, fixed syntax, and placeholder Chinese text (adjust the text to your needs):
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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" tools:context=".LoginActivity"> <EditText android:id="@+id/idText" android:layout_width="0dp" android:layout_height="53dp" android:ems="10" android:hint="请输入账号" android:inputType="textPersonName" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginHorizontal="16dp" /> <EditText android:id="@+id/pwText" android:layout_width="0dp" android:layout_height="49dp" android:ems="10" android:hint="请输入密码" android:inputType="textPassword" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/idText" android:layout_marginHorizontal="16dp" android:layout_marginTop="8dp" /> <Button android:id="@+id/loginButton" android:layout_width="0dp" android:layout_height="49dp" android:background="?attr/colorPrimary" android:text="登录" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/pwText" android:layout_marginHorizontal="16dp" android:layout_marginTop="8dp" /> <TextView android:id="@+id/registerButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="还没有账号?注册" android:textAppearance="@style/TextAppearance.AppCompat.Button" android:textSize="24sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/loginButton" android:layout_marginTop="24dp" /> </androidx.constraintlayout.widget.ConstraintLayout>
Key Changes Explained:
- Replaced
tools:layout_editor_absoluteX/Ywith realapp:layout_constraintattributes to position views relative to the parent and each other - Fixed all mismatched quotes and unclosed string issues
- Changed
android:layout_width="382dp"to0dp(match constraints) so views adapt to different screen sizes instead of using a fixed width that might be too big for some devices - Added margin for better spacing
- Replaced garbled text with clear Chinese placeholders
- Switched to
androidx.constraintlayout.widget.ConstraintLayout(the modern, supported version of ConstraintLayout—make sure your app uses AndroidX dependencies)
After applying these changes, run your app again—your EditTexts and TextView should show up correctly! Let me know if you run into any other issues.
内容的提问来源于stack exchange,提问作者Jinsung Sunwoo

