不同手机的Textview大小不同
Textview size differs for different mobiles
我先有 2 Textview
个。它在某些手机上看起来很完美,但在某些手机上它的尺寸发生了变化,它没有正确覆盖屏幕。
下面是我的 Textview
一个 580dp 和另一个 30dp.
我不能让它们匹配 parents 否则它们会相互重叠。
<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
作为视图的父视图并在视图之间设置 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>
我已经编辑了您提供的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"
android:layout_centerVertical="true"
tools:context=".Create">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:cardCornerRadius="30dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/page_data"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="58"
android:gravity="start|top"
android:padding="15dp"
android:textSize="18sp" />
<TextView
android:id="@+id/page_no_back"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_gravity="bottom"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:padding="5dp"
android:text="Page No" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
我先有 2 Textview
个。它在某些手机上看起来很完美,但在某些手机上它的尺寸发生了变化,它没有正确覆盖屏幕。
下面是我的 Textview
一个 580dp 和另一个 30dp.
我不能让它们匹配 parents 否则它们会相互重叠。
<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
作为视图的父视图并在视图之间设置 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>
我已经编辑了您提供的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"
android:layout_centerVertical="true"
tools:context=".Create">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
app:cardCornerRadius="30dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/page_data"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="58"
android:gravity="start|top"
android:padding="15dp"
android:textSize="18sp" />
<TextView
android:id="@+id/page_no_back"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_gravity="bottom"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:padding="5dp"
android:text="Page No" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>