ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 不同屏幕尺寸的约束布局

2019-07-05 21:22:39  阅读:220  来源: 互联网

标签:android user-interface constraint-layout screen-size


如何使用约束布局自动调整两个小部件之间的间隙或边距,用于不同的屏幕尺寸,例如ios.不同的屏幕尺寸可以是4.7,5.0或5.5.所有这些设备都从维度正常中选择维度,所以有没有其他方法可以自动调整两个小部件之间的边距

enter image description here

 <?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_width="match_parent"
 android:layout_height="match_parent">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="190dp"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:contentDescription="dummy"
    android:scaleType="centerCrop"
    android:src="@drawable/lion"
    app:layout_constraintBottom_creator="1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_creator="1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_creator="1"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="16dp" />

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"
    app:layout_constraintTop_toBottomOf="@+id/imageView"
    tools:layout_editor_absoluteX="8dp">

    <android.support.design.widget.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="abc" />


    <android.support.design.widget.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xyz" />
</android.support.design.widget.TabLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/til_et_email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="16dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tabLayout">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/et_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:hint="@string/et_email_hint" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/til_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="0dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/til_et_email">

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:hint="@string/et_password_hint" />
</android.support.design.widget.TextInputLayout>

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="16dp"
    android:text="@string/bt_sign_in"
    app:layout_constraintEnd_toEndOf="@+id/til_password"
    app:layout_constraintStart_toStartOf="@+id/til_password"
    app:layout_constraintTop_toBottomOf="@+id/til_password"
    tools:layout_editor_absoluteY="403dp" />

<TextView
    android:id="@+id/tv_forgot_password"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:autoSizeMaxTextSize="41sp"
    android:autoSizeMinTextSize="17sp"
    android:autoSizeStepGranularity="2sp"
    android:autoSizeTextType="uniform"
    android:gravity="center"
    android:text="@string/tv_forgot_password"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button" />

在上面的部分中,我将边缘值设置为硬编码.所以,如果我在5.0屏幕尺寸上运行代码就可以了.但是,如果我在5.5屏幕尺寸上运行它,而不是调整边距,它会在底部留下空白区域.

解决方法:

使用“指南”约束可根据不同的屏幕大小为视图提供百分比.

 <android.support.constraint.Guideline
                android:id="@+id/top_guideline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.09" />

请参阅以下谷歌链接.
https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

标签:android,user-interface,constraint-layout,screen-size
来源: https://codeday.me/bug/20190705/1391184.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有