android - 不同手机的Textview大小不同

标签 android xml textview

我首先有 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/

相关文章:

android - 有没有办法在应用程序中以编程方式捕获清除数据操作?

Android onActivityResult

php - 如何在 PHP 和 XML 中使用 foreach(simplexml)

java - XML Schema V 1.1 断言在 Xerces-J 中不起作用

xml - 对 XML 文档的各个部分进行数字签名

Android - 分隔列表字符串

java - 删除除第一个以外的所有 Activity

Android studio 模拟器(设备未经授权)

java - 如何将文字大小设置为双倍大小

android - 求行距的高度