我首先有 2 个 Textview
。它在某些手机上看起来很完美,但在某些手机上它的尺寸发生了变化,它没有正确覆盖屏幕。
下面是我的 Textview
一个 580dp 和另一个 30dp。
我不能让他们匹配 parent ,否则他们会互相重叠。
<TextView
android:id="@+id/page_data"
android:layout_width="match_parent"
android:layout_height="580dp"
android:fontFamily="sans-serif-black"
android:gravity="start|top"
android:padding="15dp" />
<TextView
android:id="@+id/page_no_back"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:padding="5dp"
android:text="Page No" />
最佳答案
您可以通过使用 ConstraintLayout
作为 View 的父 View 并在 View 之间设置 constraints
来定义关系来解决这个问题。例如检查下面的布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#d3d3d3"
tools:context=".Create">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
app:cardCornerRadius="30dp"
app:cardElevation="0dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/page_data"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fontFamily="sans-serif-black"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/page_no_back"
android:gravity="start|top"
android:padding="15dp" />
<TextView
android:id="@+id/page_no_back"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:padding="5dp"
android:text="Page No" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
关于android - 不同手机的Textview大小不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58798967/