简体   繁体   English

约束布局适用于所有屏幕

[英]Contraint layout Adapt for all screens

I am learning constraint layout. 我正在学习constraint布局。 I am new to constraint layout. 我是constraint布局的新手。 I read some tutorials and got good idea about the layout. 我阅读了一些教程,并对布局有了个好主意。 But when I try to implement the layout I am struggling to fit for all the screens. 但是,当我尝试实现布局时,我正在努力适应所有屏幕。 Here with I attached my sample screen. 在此附上我的示例屏幕。 I am designing a login screen. 我正在设计一个登录屏幕。 I want to fit this design for all the screens. 我想将这种设计适合所有屏幕。

<?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"
    tools:context="com.example.admin1.constraintlayoutsample.MainActivity"
    android:background="#E0E0E0">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="H,16:9"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/imageView"
        app:layout_constraintVertical_bias="0.128" />


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Email"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/editText"
        app:layout_constraintTop_toBottomOf="@+id/textView6"
        app:layout_constraintVertical_bias="0.10" />


    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="44dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:alpha="0.23"
        android:background="@drawable/textviewshape"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />


    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="Password"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/editText"
        app:layout_constraintTop_toBottomOf="@+id/editText"
        app:layout_constraintVertical_bias="0.100" />


    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alpha="0.23"
        android:background="@drawable/textviewshape"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />


    <Button
        android:id="@+id/button3"
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/buttonshape"
        android:text="Login"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2"
        app:layout_constraintVertical_bias="0.20"/>


</android.support.constraint.ConstraintLayout>

When I see this design in 5'inch or 6'inch screen it's looking good. 当我在5英寸或6英寸的屏幕上看到这种设计时,它看起来不错。 But when I see in 4.7 or below screen size it is not fit into the screen. 但是,当我看到4.7或以下的屏幕尺寸时,它不适合屏幕。 Please let me know how to use constraint layout for all screen sizes. 请让我知道如何对所有屏幕尺寸使用约束布局。 And please suggest some samples/tutorial for the this. 并请为此提供一些示例/教程。

5寸屏设计

3.7英寸屏幕设计

I think in order to make your screen adjustable, you can use one of several solutions: 我认为为了使屏幕可调,您可以使用以下几种解决方案之一:

  1. Make your view's height or margin in between in percents. 使视图的heightmargin在百分比之间。 Using Guideline or use weight (eg layout_constraintHorizontal_weight ). 使用Guideline或使用权重(例如layout_constraintHorizontal_weight )。 For more info check this thread . 有关更多信息,请检查此线程 And this about weight. 与体重有关。
  2. Make your image and/or text inputs, button to use different scale for different screen size. 输入您的图像和/或文本按钮,以针对不同的屏幕尺寸使用不同的比例。
  3. Make your screen scrollable if none from above is applies to your solution. 如果没有上述方法适用于您的解决方案,请使屏幕可滚动。

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

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