自定义安卓手机的横向屏幕

Posted

技术标签:

【中文标题】自定义安卓手机的横向屏幕【英文标题】:Landscape screen for custom android phone 【发布时间】:2018-11-28 04:46:08 【问题描述】:

我正在使用我的 android 手机(小米 A2 Lite,5.84 英寸,1080 x 2280,比 Nexus 5 还要大)进行应用开发。我拥有的每个布局都有横向布局,并且我对里面的所有元素都使用了 ConstraintLayouts。

但是,在我的手机上测试横向模式时,文本和图像的显示不如在 Android Studio 中正确:(

有什么解决办法吗?我可以将自己的分辨率/设备添加到设备列表中吗?

应该是这样的

这是它的显示方式

XML 景观文件的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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_
    android:layout_
    tools:context="com.example.ducnguyen.miniexplorer.MainMenu">

    <TextView
        android:id="@+id/textView11"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="@string/main_menu_title"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="@string/main_menu_title" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="gotoReadingScreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageView4"
        app:layout_constraintHorizontal_bias="0.455"
        app:layout_constraintStart_toEndOf="@+id/imageView5"
        app:layout_constraintTop_toBottomOf="@+id/textView11"
        app:layout_constraintVertical_bias="0.092"
        app:srcCompat="@drawable/forest" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:layout_marginEnd="48dp"
        android:layout_marginBottom="8dp"
        android:onClick="gotoExplorerScreen"
        app:layout_constraintBottom_toTopOf="@+id/textView13"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.756"
        app:srcCompat="@drawable/car" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_
        android:layout_
        android:layout_marginStart="68dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="192dp"
        android:onClick="gotoSettingScreen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageView4"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/settings" />

    <ImageView
        android:id="@+id/imageView10"

        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginEnd="56dp"
        android:layout_marginBottom="56dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.551"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/phonecall" />

    <ImageView
        android:id="@+id/imageView12"
        android:onClick="LogOut"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="48dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.879"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/logout" />

    <TextView
        android:id="@+id/textView12"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="gotoReadingScreen"
        android:text="@string/menu_reading"
        app:layout_constraintBottom_toTopOf="@+id/imageView10"
        app:layout_constraintEnd_toStartOf="@+id/textView13"
        app:layout_constraintStart_toEndOf="@+id/textView14"
        app:layout_constraintTop_toBottomOf="@+id/imageView3"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView13"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="184dp"
        android:layout_marginEnd="8dp"
        android:onClick="gotoExplorerScreen"
        android:text="@string/menu_explorer_status"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.891"
        app:layout_constraintStart_toEndOf="@+id/textView14"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView14"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="gotoSettingScreen"
        android:text="@string/menu_settings"
        android:textSize="15sp"
        app:layout_constraintBottom_toTopOf="@+id/imageView11"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.147"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView5"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView15"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/menu_live_support"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView10"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textView16"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="gotoAboutUsScreen"
        android:text="@string/menu_about_us"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textView15"
        app:layout_constraintHorizontal_bias="0.454"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView11"
        app:layout_constraintVertical_bias="0.333" />

    <TextView
        android:id="@+id/textView17"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="gotoLoginScreen"
        android:text="@string/logout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.709"
        app:layout_constraintStart_toEndOf="@+id/textView15"
        app:layout_constraintTop_toBottomOf="@+id/imageView12"
        app:layout_constraintVertical_bias="0.0" />

    <ImageView
        android:id="@+id/imageView11"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="8dp"
        android:onClick="gotoAboutUsScreen"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.155"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView5"
        app:srcCompat="@drawable/question" />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

能否发一些截图和代码 @NoName 我加了他们 【参考方案1】:

试试这个示例布局。它适用于 Mi A2 设备

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_
     android:layout_
     xmlns:app="http://schemas.android.com/apk/res-auto"
     tools:context=".MainActivity"
     android:background="#212c3d">

<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView11"
        android:layout_
        android:layout_
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="Main Menu"
        android:textSize="30sp"
        android:textColor="#FFF"
        android:layout_margin="10dp"
        android:layout_gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>


    <LinearLayout
        android:layout_
        android:layout_
        android:weightSum="3"
        android:layout_margin="10dp">

        <LinearLayout
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.551"
            app:layout_constraintStart_toStartOf="parent">
            <ImageView
                android:layout_
                android:layout_
                android:src="@drawable/icon_setting" />
            <TextView
                android:layout_
                android:layout_
                android:text="Settings"
                android:textColor="#FFF" />
        </LinearLayout>

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1">
            <ImageView
                android:layout_
                android:layout_
                android:src="@drawable/icon_setting"/>
            <TextView
                android:layout_
                android:layout_
                android:text="Settings"
                android:textColor="#FFF"/>
        </LinearLayout>

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1">
            <ImageView
                android:layout_
                android:layout_
                android:src="@drawable/icon_setting"/>
            <TextView
                android:layout_
                android:layout_
                android:text="Settings"
                android:textColor="#FFF"/>
        </LinearLayout>

    </LinearLayout>


    <LinearLayout
        android:layout_
        android:layout_
        android:weightSum="3"
        android:layout_margin="10dp">

        <LinearLayout
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">
            <ImageView
                android:layout_
                android:layout_
                android:src="@drawable/icon_setting" />
            <TextView
                android:layout_
                android:layout_
                android:text="Settings"
                android:textColor="#FFF" />
        </LinearLayout>

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1">
            <ImageView
                android:layout_
                android:layout_
                android:src="@drawable/icon_setting"/>
            <TextView
                android:layout_
                android:layout_
                android:text="Settings"
                android:textColor="#FFF"/>
        </LinearLayout>

        <LinearLayout
            android:layout_
            android:layout_
            android:orientation="vertical"
            android:gravity="center"
            android:layout_weight="1">
            <ImageView
                android:layout_
                android:layout_
                android:src="@drawable/icon_setting"/>
            <TextView
                android:layout_
                android:layout_
                android:text="Settings"
                android:textColor="#FFF"/>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

示例截图

【讨论】:

以上是关于自定义安卓手机的横向屏幕的主要内容,如果未能解决你的问题,请参考以下文章

怎样设置安卓手机时间

安卓手机如何自定义设置动态壁纸

横向模式 - 安卓

Unity之手机键盘自定义输入栏位置适配&不同手机分辨率适配

Unity之手机键盘自定义输入栏位置适配&不同手机分辨率适配

Unity之手机键盘自定义输入栏位置适配&不同手机分辨率适配