{"id":13537534,"url":"https://github.com/hongyangAndroid/AndroidAutoLayout","last_synced_at":"2025-04-02T04:31:00.094Z","repository":{"id":143458829,"uuid":"46461070","full_name":"hongyangAndroid/AndroidAutoLayout","owner":"hongyangAndroid","description":"[停止维护]Android屏幕适配方案，直接填写设计图上的像素尺寸即可完成适配，最大限度解决适配问题。","archived":false,"fork":false,"pushed_at":"2018-11-26T13:24:38.000Z","size":7466,"stargazers_count":6676,"open_issues_count":132,"forks_count":1895,"subscribers_count":339,"default_branch":"master","last_synced_at":"2025-03-28T18:06:12.280Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://blog.csdn.net/lmj623565791/article/details/49990941","language":"Java","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hongyangAndroid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-11-19T02:09:11.000Z","updated_at":"2025-03-27T09:38:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"37638543-49ee-4aaf-b912-c00c88ace449","html_url":"https://github.com/hongyangAndroid/AndroidAutoLayout","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hongyangAndroid%2FAndroidAutoLayout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hongyangAndroid%2FAndroidAutoLayout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hongyangAndroid%2FAndroidAutoLayout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hongyangAndroid%2FAndroidAutoLayout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hongyangAndroid","download_url":"https://codeload.github.com/hongyangAndroid/AndroidAutoLayout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246757023,"owners_count":20828813,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T09:01:00.188Z","updated_at":"2025-04-02T04:30:55.082Z","avatar_url":"https://github.com/hongyangAndroid.png","language":"Java","readme":"# AndroidAutoLayout\n[DEPRECATED]Android屏幕适配方案，直接填写设计图上的像素尺寸即可完成适配。\n\n\u003e目前没有精力，已停止维护，使用前务必看明白代码，明确该方案可以解决自身问题，有扩展代码能力，否则不建议使用。\n\n非常感谢 ： 吃土豆的人 的协作。\n\n\n\u003e推荐使用[AndroidAutoSize](https://github.com/JessYanCoding/AndroidAutoSize)\n\nAndroidAutoSize目前作者一直在维护，私下也有一些交流，也是 AndroidAutoLayout 3年的使用者，自研了[AndroidAutoSize](https://github.com/JessYanCoding/AndroidAutoSize), 在目前本库已经没有精力维护的情况下，推荐使用；如果使用了本库，迁移成本也非常低,[迁移指南](https://github.com/JessYanCoding/AndroidAutoSize/issues/90)。\n\n\n\n## 效果图\n\n最大幅度解决适配问题，并且最大化方便开发者。\n\n\n\nso,看下用法：\n\n\u003cimg src=\"autolayout_08.png\"/\u003e\n\n\n\u003cimg src=\"autolayout_09.png\"/\u003e\n\n你没有看错，拿到设计稿，在布局文件里面直接填写对应的px即可，px:这里的px并非是Google不建议使用的px，在内部会进行转化处理。\n\nok，拿一些实际项目的页面，看下不同分辨率下的效果：\n\n左为：768 * 1280 ; 右为：1080 * 1920\n\n\u003cimg src=\"preview/preview_01.png\" width=\"800px\"/\u003e\n\n\u003cimg src=\"preview/preview_02.png\" width=\"800px\"/\u003e\n\n\u003cimg src=\"preview/preview_03.png\" width=\"800px\"/\u003e\n\n\n\n上述两个机器的分辨率差距挺大了，但是完美实现了适配，最为重要的是：\n\n* 再也不用拿着设计稿去想这控件的宽高到底取多少dp\n* 再也不用去为多个屏幕去写多个dimens\n* 再也不用去计算百分比了（如果使用百分比控件完成适配）\n* 再也不用去跟UI MM去解释什么是dp了\n\n你所要做的就是抄抄设计稿上面的px，直接写入布局文件。\n\n还有很多好处，比如上面的Item里面元素比较多，如果标识的比较全面，一个FrameLayout，里面的View填写各种marginLeft,marginTop就能完美实现，几乎不需要嵌套了。\n\n## 引入\n\n* Android Studio\n\n将[autolayout](autolayout)引入\n\n```xml\ndependencies {\n    compile project(':autolayout')\n}\n```\n\n也可以直接\n\n```\ndependencies {\n    compile 'com.zhy:autolayout:1.4.5'\n}\n```\n\n* Eclipse\n\n建议使用As，方便版本更新。实在不行，只有复制粘贴源码了。\n\n## 用法\n\n### 第一步：\n\n在你的项目的AndroidManifest中注明你的`设计稿`的尺寸。\n\n```xml\n\u003cmeta-data android:name=\"design_width\" android:value=\"768\"\u003e\n\u003c/meta-data\u003e\n\u003cmeta-data android:name=\"design_height\" android:value=\"1280\"\u003e\n\u003c/meta-data\u003e\n    \n```\n\n### 第二步：\n\n让你的Activity继承自`AutoLayoutActivity`.\n\n\n非常简单的两个步骤，你就可以开始愉快的编写布局了，详细可以参考sample。\n\n\n\n## 其他用法\n\n如果你不希望继承`AutoLayoutActivity`，可以在编写布局文件时，将\n\n* LinearLayout -\u003e AutoLinearLayout\n* RelativeLayout -\u003e AutoRelativeLayout\n* FrameLayout -\u003e AutoFrameLayout\n\n这样也可以完成适配。\n\n## 目前支持属性\n\n* layout_width\n* layout_height\n* layout_margin(left,top,right,bottom)\n* pading(left,top,right,bottom)\n* textSize\n* maxWidth, minWidth, maxHeight, minHeight\n\n\n## 配置\n\n默认使用的高度是设备的可用高度，也就是不包括状态栏和底部的操作栏的，如果你希望拿设备的物理高度进行百分比化：\n\n可以在Application的onCreate方法中进行设置:\n\n```java\npublic class UseDeviceSizeApplication extends Application\n{\n    @Override\n    public void onCreate()\n    {\n        super.onCreate();\n        AutoLayoutConifg.getInstance().useDeviceSize();\n    }\n}\n\n```\n\n## 预览\n\n大家都知道，写布局文件的时候，不能实时的去预览效果，那么体验真的是非常的不好，也在很大程度上降低开发效率，所以下面教大家如何用好，用对PreView（针对该库）。\n\n首先，你要记得你设计稿的尺寸，比如 `768 * 1280`\n\n然后在你的PreView面板，选择于设计图分辨率一致的设备：\n\n\n\u003cimg src=\"autolayout_07.png\" width=\"480px\"/\u003e\n\n然后你就可以看到`最为精确的`预览了：\n\n\u003cimg src=\"autolayout_06.png\" width=\"480px\"/\u003e\n\n两个注意事项：\n\n1. 你们UI给的设计图的尺寸并非是主流的设计图，该尺寸没找到，你可以自己去新建一个设备。\n2. 不要在PreView中去查看所有分辨率下的显示，是看不出来适配效果的，因为有些计算是动态的。\n\n## 扩展\n\n对于其他继承系统的FrameLayout、LinearLayout、RelativeLayout的控件，比如`CardView`，如果希望再其内部直接支持\"px\"百分比化，可以自己扩展，扩展方式为下面的代码，也可参考[issue#21](https://github.com/hongyangAndroid/AndroidAutoLayout/issues/21)：\n\n```\npackage com.zhy.sample.view;\n\nimport android.content.Context;\nimport android.support.v7.widget.CardView;\nimport android.util.AttributeSet;\n\nimport com.zhy.autolayout.AutoFrameLayout;\nimport com.zhy.autolayout.utils.AutoLayoutHelper;\n\n/**\n * Created by zhy on 15/12/8.\n */\npublic class AutoCardView extends CardView\n{\n    private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this);\n\n    public AutoCardView(Context context)\n    {\n        super(context);\n    }\n\n    public AutoCardView(Context context, AttributeSet attrs)\n    {\n        super(context, attrs);\n    }\n\n    public AutoCardView(Context context, AttributeSet attrs, int defStyleAttr)\n    {\n        super(context, attrs, defStyleAttr);\n    }\n\n    @Override\n    public AutoFrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs)\n    {\n        return new AutoFrameLayout.LayoutParams(getContext(), attrs);\n    }\n\n    @Override\n    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)\n    {\n        if (!isInEditMode())\n        {\n            mHelper.adjustChildren();\n        }\n        super.onMeasure(widthMeasureSpec, heightMeasureSpec);\n    }\n\n\n}\n```\n\n\n## 注意事项\n\n### ListView、RecyclerView类的Item的适配\n\n**sample中包含ListView、RecyclerView例子，具体查看sample**\n\n* 对于ListView\n\n对于ListView这类控件的item，默认根局部写“px”进行适配是无效的，因为外层非AutoXXXLayout，而是ListView。但是，不用怕，一行代码就可以支持了：\n\n```java\n@Override\npublic View getView(int position, View convertView, ViewGroup parent)\n{\n    ViewHolder holder = null;\n    if (convertView == null)\n    {\n        holder = new ViewHolder();\n        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);\n        convertView.setTag(holder);\n        //对于listview，注意添加这一行，即可在item上使用高度\n        AutoUtils.autoSize(convertView);\n    } else\n    {\n        holder = (ViewHolder) convertView.getTag();\n    }\n\n    return convertView;\n}\n```\n\n注意` AutoUtils.autoSize(convertView);`这行代码的位置即可。demo中也有相关实例。\n\n\n* 对于RecyclerView\n\n```java\npublic ViewHolder(View itemView)\n{\n      super(itemView);\n      AutoUtils.autoSize(itemView);\n}\n\n//...\n@Override\npublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)\n{\n     View convertView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item, parent, false);\n     return new ViewHolder(convertView);\n}\n\n```\n\n一定要记得`LayoutInflater.from(mContext).inflate`使用三个参数的方法！\n\n\n### 指定设置的值参考宽度或者高度\n\n由于该库的特点，布局文件中宽高上的1px是不相等的，于是如果需要宽高保持一致的情况，布局中使用属性：\n\n `app:layout_auto_basewidth=\"height\"`，代表height上编写的像素值参考宽度。\n \n `app:layout_auto_baseheight=\"width\"`，代表width上编写的像素值参考高度。\n\n如果需要指定多个值参考宽度即：\n\n`app:layout_auto_basewidth=\"height|padding\"`\n\n用|隔开，类似gravity的用法，取值为：\n\n* width,height\n* margin,marginLeft,marginTop,marginRight,marginBottom\n* padding,paddingLeft,paddingTop,paddingRight,paddingBottom\n* textSize.\n\n### TextView的高度问题\n\n设计稿一般只会标识一个字体的大小，比如你设置textSize=\"20px\"，实际上TextView所占据的高度肯定大于20px，字的上下都会有一定的间隙，所以一定要灵活去写字体的高度，比如对于text上下的margin可以选择尽可能小一点。或者选择别的约束条件去定位（比如上例，选择了marginBottom）\n\n\n## 常见问题\n\n###(1)导入后出现`org/gradle/api/publication/maven/internal/DefaultMavenFactory`\n\n最简单的方式，通过`compile 'com.zhy:autolayout:x.x.x'`进行依赖使用，如果一定要以module引用，参考该issue[#74](https://github.com/hongyangAndroid/AndroidAutoLayout/issues/74)\n\n###(2)RadioGroup,Toolbar等控件中的子View无法完成适配\n\n这个其实上文已经提到过了，需要自己扩展。不过这个很多使用者贡献了他们的扩展类可以直接使用，\n参考[autolayout-widget](https://github.com/hongyangAndroid/AndroidAutoLayout/tree/master/widgetsample),\n如果没有发现你需要的容器类，那么你就真的需要自行扩展了，当然如果你完成了扩展，可以给我发个PR，或者让我知道，我可以加入到\n`autolayout-widget`中方便他人，ps:需要用到哪个copy就好了，不要直接引用`autolayout-widget`，因为其引用了大量的库，可能很多\n库你是用不到的。\n\n###(3)java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.\n\n这个问题是因为默认AutoLayoutActivity会继承自AppCompatActivity，所以默认需要设置\nTheme.AppCompat的theme；\n\n如果你使用的依旧是FragmentActivity等，且不考虑使用AppCompatActivity，\n你可以选择自己编写一个`MyAutoLayoutActivity extends 目前你使用的Activity基类`，例如\n`MyAutoLayoutActivity extends FragmentActivity`，然后将该库中AutoLayoutActivity中的逻辑\n拷贝进去即可，以后你就继承你的`MyAutoLayoutActivity`就好了。\n\nps：还是建议尽快更新SDK版本使用`AppCompatActivity`.\n\n\n\n## 其他信息\n\n作者信息：\n\n* [hongyangAndroid](https://github.com/hongyangAndroid)\n* 吃土豆的人\n\n\n灵感来自：\n\n* [android-percent-support-lib-sample](https://github.com/JulienGenoud/android-percent-support-lib-sample)\n* [android-percent-support-extend](https://github.com/hongyangAndroid/android-percent-support-extend)\n* [Android 屏幕适配方案](http://blog.csdn.net/lmj623565791/article/details/45460089)\n\n\n\n\n\n\n\n","funding_links":[],"categories":["布局","Java"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FhongyangAndroid%2FAndroidAutoLayout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FhongyangAndroid%2FAndroidAutoLayout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FhongyangAndroid%2FAndroidAutoLayout/lists"}