java - 如何缩放粉色框,使其完美适合电子表格的网格,无论手机屏幕尺寸如何?安卓工作室

标签 java android android-studio kotlin

Image

粉色框是表格布局,电子表格是约束布局。我尝试过使用边距和约束,但是当我在不同的手机上运行它时,粉红色的盒子总是在外面或者太小。现在的代码:

<?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"
    android:layout_centerHorizontal="false"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/game1"
    android:maxWidth="500dp">

    <TableLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="44dp"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="147dp"
        android:layout_marginEnd="2dp"
        android:layout_marginRight="1dp"
        android:layout_marginBottom="51dp"
        android:layout_weight="1"
        android:foreground="#CBD81B60"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:visibility="visible">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

您可以像这样使用constraintlayout.widget.Guideline并使用layout_constraintGuide_percent

<?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"
    android:layout_centerHorizontal="false"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/game1">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".2"/>

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".1"/>
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guide_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".9"/>

    <TableLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:foreground="#CBD81B60"
        app:layout_constraintBottom_toBottomOf="@id/guide_bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="@id/guide_start"
        app:layout_constraintTop_toTopOf="@id/guide_top"
        app:layout_constraintVertical_bias="0.0"
        tools:visibility="visible">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

您可以根据需要更改layout_constraintGuide_percent

希望这有帮助

关于java - 如何缩放粉色框,使其完美适合电子表格的网格,无论手机屏幕尺寸如何?安卓工作室,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503612/

相关文章:

java - Android Studio应用程序文本格式

java - 以最小的质量损失减小 android 中的视频大小

android - FILL_AND_STROKE 的意义何在?

安卓 : Run Card View and Recycler view in lower version

android-studio - Flutter: 'package get' 还没有运行; 'Pub get' 还没有运行

java - Eclipse 或 Javac 错误; lambda 类型推断

java - 在我的 Java 数组分类代码中找不到问题

android - 我无法使用 Espresso 单击复选框

java - IF 语句给出无法解析 PDFDocument 中的符号

Android Studio 看不到 Android 进程