简体   繁体   English

支持不同的屏幕尺寸

[英]Supporting different screen sizes

My UI fits my virtual device fine but when I run the app on my phone, the UI is too wide and I lose a portion off the RHS of my device. 我的UI非常适合我的虚拟设备,但是当我在手机上运行应用程序时,用户界面太宽,而且我的设备的RHS丢失了一部分。

I read this article;( https://developer.android.com/training/multiscreen/screensizes.html ) It talks of using a constraint layout but before I go and make wholesale changes to my XML, can anyone see any glaring flaws? 我读了这篇文章;( https://developer.android.com/training/multiscreen/screensizes.html )它谈到了使用约束布局,但在我去批量更改我的XML之前,有人能看到任何明显的缺陷吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.wbc.TabFragment0"
    android:id="@+id/frag7"
    android:padding="5dp">

    <TextView
        android:id="@+id/todayDate0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="25sp"/>


    <TableLayout
        android:id="@+id/tab_layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/todayDate0">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp">

            <TextView
                android:id="@+id/rink1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="1"
                android:textSize="30sp" />

            <Button
                android:id="@+id/twelve7R1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="bookRink"
                android:tag="7112"
                android:text="12:00"
                tools:ignore="OnClick" />

            <Button
                android:id="@+id/two7R1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="bookRink"
                android:tag="7114"
                android:text="14:00"
                tools:ignore="OnClick" />

            <Button
                android:id="@+id/four7R1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="bookRink"
                android:tag="7116"
                android:text="16:00"
                tools:ignore="OnClick" />

            <Button
                android:id="@+id/six7R1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="bookRink"
                android:tag="7118"
                android:text="18:00"
                tools:ignore="OnClick" />
        </TableRow>


    </TableLayout>
</RelativeLayout>

Please post the picture which shows the desired result for your Layout File, Here I assumed the result layout as in the Image. 请发布显示布局文件所需结果的图片,在此我假设结果布局如图像中所示。

Result Layout 结果布局

I used ConstraintLayout which can adjust the Views at runtime, This helps the device to adjust the layout according to any device screen size 我使用ConstraintLayout可以在运行时调整视图,这有助于设备根据任何设备屏幕大小调整布局

ConstraintLayout as per Google ConstraintLayout根据谷歌

<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:orientation="horizontal"
android:id="@+id/frag7"
android:padding="5dp">

<TextView
    android:id="@+id/todayDate0"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:text="Today's Date 4.06.19"
    app:layout_constraintEnd_toEndOf="@+id/tab_layout1"
    app:layout_constraintStart_toStartOf="@+id/tab_layout1"
    app:layout_constraintTop_toTopOf="parent" />


<TableLayout
    android:id="@+id/tab_layout1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/todayDate0"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.307"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/todayDate0">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <TextView
            android:id="@+id/rink1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="1"
            android:textSize="30sp" />

        <Button
            android:id="@+id/twelve7R1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="bookRink"
            android:tag="7112"
            android:text="12:00"
            tools:ignore="OnClick" />

        <Button
            android:id="@+id/two7R1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="bookRink"
            android:tag="7114"
            android:text="14:00"
            tools:ignore="OnClick" />

        <Button
            android:id="@+id/four7R1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="bookRink"
            android:tag="7116"
            android:text="16:00"
            tools:ignore="OnClick" />

        <Button
            android:id="@+id/six7R1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="bookRink"
            android:tag="7118"
            android:text="18:00"
            tools:ignore="OnClick" />
    </TableRow>


</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

感谢您的帖子,无论出于何种原因,它现在正在工作,除了将APK重新加载到我的手机上之外,我没有做任何更改......为浪费您的时间道歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM