弹性布局和AndroidAutoSize屏幕适配

弹性布局和AndroidAutoSize屏幕适配导言:技术在不断的革新,需要有新的技术代替老的方案,老的不再维护,新的一直推荐,作为技术也不能一直停滞不前概念:弹性布局(约束布局):ConstraintLayout可以算是RelativeLayout的升级版屏幕适配:百分比和最小宽度从px的AndroidAutoLayout方案到dp,pm等主副单位AndroidAutoSize方案案例步骤:1:ConstraintLayo…

大家好,又见面了,我是你们的朋友全栈君。

导言:
技术在不断的革新,需要有新的技术代替老的方案,老的不再维护,新的一直推荐,作为技术也不能一直停滞不前

概念:
弹性布局(约束布局): ConstraintLayout可以算是RelativeLayout的升级版
屏幕适配: 百分比和最小宽度从px的AndroidAutoLayout方案到dp,pm等主副单位AndroidAutoSize方案

案例步骤:
1:ConstraintLayout(弹性布局(约束布局)):
基本属性:crr,clr,ctb等等,按照400×640设计图dp编写

<?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="@dimen/dp400"
    android:layout_height="wrap_content"
    tools:context="MainActivity">

    <com.youth.banner.Banner
        android:id="@+id/main_banner"
        android:layout_width="wrap_content"
        android:layout_height="315dp"
        android:layout_alignParentTop="true"
        android:background="#fff"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:alpha="0.2"
        android:background="#FFFFFF"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="17dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="7dp"
        android:src="@drawable/logo_main"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/main_exit_dialog_iv"
        android:layout_width="28dp"
        android:layout_height="18dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/back_button_main"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/main_notice_iv"
        android:layout_width="18dp"
        android:layout_height="10dp"
        android:layout_marginLeft="@dimen/dp16"
        android:layout_marginTop="@dimen/dp10"
        android:background="@drawable/notice"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_banner" />

    <TextView
        android:id="@+id/main_notice_detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp10"
        android:layout_marginTop="@dimen/dp5"
        android:text="@string/lottery_notice"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/main_notice_iv"
        app:layout_constraintTop_toBottomOf="@+id/main_banner" />

    <ImageView
        android:id="@+id/main_image_iv"
        android:layout_width="171dp"
        android:layout_height="256dp"
        android:layout_marginLeft="@dimen/dp16"
        android:layout_marginTop="@dimen/dp5"
        android:src="@drawable/main_image"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_notice_iv" />

    <Button
        android:id="@+id/main_p3_bt"
        android:layout_width="55dp"
        android:layout_height="@dimen/dp32"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="@dimen/dp5"
        android:background="@drawable/button_select_p3"
        tools:layout_editor_absoluteX="200dp"
        tools:layout_editor_absoluteY="346dp"
        app:layout_constraintLeft_toRightOf="@+id/main_image_iv"
        app:layout_constraintTop_toBottomOf="@+id/main_notice_detail"/>

    <Button
        android:id="@+id/main_p5_bt"
        android:layout_width="@dimen/dp55"
        android:layout_height="32dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="@dimen/dp5"
        android:background="@drawable/button_select_p5"
        app:layout_constraintLeft_toRightOf="@+id/main_p3_bt"
        app:layout_constraintTop_toBottomOf="@id/main_notice_iv" />

    <Button
        android:id="@+id/main_p10_bt"
        android:layout_width="55dp"
        android:layout_height="32dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="@dimen/dp5"
        android:background="@drawable/button_select_p10"
        app:layout_constraintLeft_toRightOf="@id/main_p5_bt"
        app:layout_constraintTop_toBottomOf="@id/main_notice_iv" />

    <TextView
        android:id="@+id/main_ten_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp17"
        android:layout_marginTop="@dimen/dp13"
        android:text="bbbbbbbbb"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@id/main_image_iv"
        app:layout_constraintTop_toBottomOf="@id/main_p3_bt" />

    <TextView
        android:id="@+id/main_ticket_num_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp19"
        android:layout_marginRight="@dimen/dp19"
        android:includeFontPadding="false"
        android:text="0"
        android:textColor="@color/result_view"
        android:textSize="80sp"
        android:textStyle="bold"
        app:layout_constraintLeft_toRightOf="@+id/main_image_iv"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_ten_tv" />

    <Button
        android:id="@+id/main_sub_bt"
        android:layout_width="@dimen/dp60"
        android:layout_height="@dimen/dp60"
        android:layout_marginLeft="@dimen/dp13"
        android:layout_marginTop="@dimen/dp63"
        android:background="@drawable/button_select_sub"
        app:layout_constraintLeft_toRightOf="@+id/main_image_iv"
        app:layout_constraintTop_toBottomOf="@id/main_ten_tv" />

    <Button
        android:id="@+id/main_add_bt"
        android:layout_width="@dimen/dp60"
        android:layout_height="@dimen/dp60"
        android:layout_marginLeft="@dimen/dp13"
        android:layout_marginRight="@dimen/dp13"
        android:layout_marginTop="@dimen/dp63"
        android:background="@drawable/button_select_add"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_ten_tv" />

    <TextView
        android:id="@+id/main_xjf_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp22"
        android:layout_marginTop="5dp"
        android:text="xxx"
        android:textColor="@color/result_view"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/main_image_iv"
        app:layout_constraintTop_toBottomOf="@id/main_sub_bt" />

    <TextView
        android:id="@+id/main_payment_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="0"
        android:textColor="@color/color_006"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/main_xjf_tv"
        app:layout_constraintTop_toBottomOf="@id/main_sub_bt" />

    <TextView
        android:id="@+id/main_yuan_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="aaa"
        android:textColor="@color/result_view"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/main_payment_tv"
        app:layout_constraintTop_toBottomOf="@id/main_sub_bt" />

    <TextView
        android:id="@+id/main_yuxia_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp15"
        android:layout_marginTop="5dp"
        android:text="yyyyy:"
        android:textColor="@color/result_view"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/main_yuan_tv"
        app:layout_constraintTop_toBottomOf="@id/main_sub_bt" />

    <TextView
        android:id="@+id/main_ticket_surplus_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="0"
        android:textColor="@color/color_006"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/main_yuxia_tv"
        app:layout_constraintTop_toBottomOf="@id/main_sub_bt" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="aaa"
        android:textColor="@color/result_view"
        android:textSize="12sp"
        app:layout_constraintLeft_toRightOf="@+id/main_ticket_surplus_tv"
        app:layout_constraintTop_toBottomOf="@id/main_sub_bt" />

    <Button
        android:id="@+id/main_go_payment_bt"
        android:layout_width="186dp"
        android:layout_height="43dp"
        android:layout_marginLeft="@dimen/dp13"
        android:layout_marginTop="@dimen/dp5"
        android:background="@drawable/go_buy"
        app:layout_constraintLeft_toRightOf="@+id/main_image_iv"
        app:layout_constraintTop_toBottomOf="@id/main_xjf_tv" />

    <TextView
        android:id="@+id/main_device_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp15"
        android:layout_marginTop="@dimen/dp10"
        android:text="aaaaa: "
        android:textSize="11sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/main_image_iv" />

    <TextView
        android:id="@+id/main_device_serial_num_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp10"
        android:text="00000000"
        android:textSize="11sp"
        app:layout_constraintLeft_toRightOf="@+id/main_device_tv"
        app:layout_constraintTop_toBottomOf="@+id/main_image_iv" />

</android.support.constraint.ConstraintLayout>

2:AndroidAutoSize
2.1:导包

implementation 'me.jessyan:autosize:1.1.0'

2.2:配置androidmainfest,这个是按照UI给的设计图

 <meta-data
            android:name="design_width_in_dp"
            android:value="400" />
        <meta-data
            android:name="design_height_in_dp"
            android:value="640" />

2.3:其他API
CustomAdapt:修改当前页面的设计图尺寸

public class MainActivity extends AppCompatActivity  implements CustomAdapt  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
    }
     // @return {@code true} 为按照宽度进行适配, {@code false} 为按照高度进行适配
    @Override
    public boolean isBaseOnWidth() {
        return true;
    }
//根据上面这个赋值,比如原图是宽度400适配,这个改成360宽度
    @Override
    public float getSizeInDp() {
        return 360;
    }
}

CancelAdapt :保存原样,放弃自动适配

ok,结束,以后就用这个.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/136557.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 七年级小四门知识点_unity animator trigger

    七年级小四门知识点_unity animator triggerUnity小科普老规矩,先介绍一下Unity的科普小知识:Unity是实时3D互动内容创作和运营平台。包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助Unity将创意变成现实。Unity平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。也可以简单把Unity理解为一个游戏引擎,可以用来专业制作游戏!Unity小知识点学习Unity中使用代码查询D.

    2026年1月27日
    8
  • Http通过header传递参数_http contenttype

    Http通过header传递参数_http contenttype提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、header常用指令header分为三部分:发送一个200正常响应set404header:页面没找到页面被永久删除,可以告诉搜索引擎更新它们的urls访问受限服务器错误重定向到一个新的位置延迟一段时间后重定向覆盖X-Powered-Byvalue内容语言(en=English)最后修改时间(在缓存的时候可以用到)告诉浏览器要获取的内容还没有更新设置内容的长度(缓存的时候可以用到):用来下载文件:禁止

    2022年8月24日
    8
  • 解决ubuntu16.04中codeblocks中文显示不全的问题[通俗易懂]

    解决ubuntu16.04中codeblocks中文显示不全的问题[通俗易懂]ubuntu16.04中安装中文库、中文输入法、搜狗输入法、解决终端无法显示中文的问题、安装字体(YaheiConsolas字体)、更换漂亮绚丽flatbulous主题。codeblock设置字体为:kacstdigital或centuryschoolbookl解决中文注释显示不全的问题,修改codeblocks丑陋的运行窗口为ubuntu16.04默认的终端。

    2022年7月26日
    19
  • maven – filtering标签

    maven – filtering标签方式一 首先在 pom xml 文件中做出以下添加 nbsp lt project gt lt name gt HelloWorld lt name gt lt build gt lt resources gt lt resource gt lt directory gt src m

    2025年11月23日
    5
  • NMAKE编译CTK[通俗易懂]

    NMAKE编译CTK[通俗易懂]NMAKE编译CTK启动编译环境从VC中启动命令行或通过VC提供的批处理启动命令行,以能运行编译环境。如果装了多个VC版本,注意使用想要的VC版本启动安装编译环境。[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5YlX5Gta-1597631378020)(study/image-20200815174602043.png)]再启动cmaked:\soft\ProgramFiles(x86)\MicrosoftVisualtudio\2017\Commun

    2022年6月5日
    55
  • 《微机原理与接口技术》简答题总结及答案_微机原理与接口技术试题及答案

    《微机原理与接口技术》简答题总结及答案_微机原理与接口技术试题及答案文章目录重点:一、8086/8088二、半导体存储器三、寻址方式四、汇编五、中断技术六、输入输出重点:8086/8088、寻址方式、汇编、中断、8253、8255、8259一、8086/80881、简述微机的组成及功能★微机主要有存储器、I/O设备和I/O接口、CPU、系统总线、操作系统和应用软件组成,各部分功能如下:CPU:统一协调和控制系统中的各个部件系统总线:传送信息存储器:存放程序和数据I/O设备:实现微机的输入输出功能I/O接口:I/O设备与CPU的桥梁操作系统:管理系

    2022年9月28日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号