android适配解决方案
2015-12-17 15:05
447 查看
看了篇大神的文章,感觉用处很大,附上转载地址
http://blog.csdn.net/lmj623565791/article/details/49990941
主要为了解决android让人头疼的适配,大家可以看原文,也可以看以下内容,我会简单记录下用法
以前代码一直是用dp,美工则大爱px,所以还要转换太烦人,用这种方法布局文件则可以用px了
首先可以建一个放这些工具代码的包,里面就这些类,大家可以去下载(代码中如果出现了Object不能强转为int的错误请改为Integer或更换高版本jdk)
http://download.csdn.net/detail/qq707548235/9364809
然后需要在主activity中加如下代码
private static final String LAYOUT_LINEARLAYOUT = "LinearLayout";
private static final String LAYOUT_FRAMELAYOUT = "FrameLayout";
private static final String LAYOUT_RELATIVELAYOUT = "RelativeLayout";
在oncreate中加上一句话就行
AutoLayout.getInstance().auto(this);
重写Activity的onCreateView方法
@Override
public View onCreateView(String name, Context context, AttributeSet attrs)
{
View view = null;
if (name.equals(LAYOUT_FRAMELAYOUT))
{
view = new AutoFrameLayout(context, attrs);
}
if (name.equals(LAYOUT_LINEARLAYOUT))
{
view = new AutoLinearLayout(context, attrs);
}
if (name.equals(LAYOUT_RELATIVELAYOUT))
{
view = new AutoRelativeLayout(context, attrs);
}
if (view != null) return view;
return super.onCreateView(name, context, attrs);
}
主方法写这些就可以了,然后要在配置文件中加点东西,在AndroidManifest.xml的application标签中加
<meta-data
android:name="design_width"
android:value="240"></meta-data>
<meta-data
android:name="design_height"
android:value="320"></meta-data>
value值按需更改,和美工设计时采用的分辨率一致,比如上面的意思就是美工是按分辨率240X320的手机为模版进行标注px的
在res/values文件加下的attrs.xml(没有就自己建一个),里面代码如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyDownView">
<attr name="backgroud" format="reference" />
</declare-styleable>
<declare-styleable name="AutoLayout_Layout">
<attr name="layout_auto_textSizeBaseWidth" format="boolean"/>
</declare-styleable>
</resources>
做完这些就可以告一段落,我们在布局文件中有可以安心使用px了,即使是px在不同分辨率的手机上显示比例也是一样的
注意:在studio中可行,eclipse可能还需要额外配置些东西,github上介绍的很详细
https://github.com/hongyangAndroid/AndroidAutoLayout
我在手机上大概试了试,基本是没问题(请原谅我没多次测试,有兴趣的可以各种分辨率都试试)
最后依旧是感谢大家阅读,欢迎大神们指点。
http://blog.csdn.net/lmj623565791/article/details/49990941
主要为了解决android让人头疼的适配,大家可以看原文,也可以看以下内容,我会简单记录下用法
以前代码一直是用dp,美工则大爱px,所以还要转换太烦人,用这种方法布局文件则可以用px了
首先可以建一个放这些工具代码的包,里面就这些类,大家可以去下载(代码中如果出现了Object不能强转为int的错误请改为Integer或更换高版本jdk)
http://download.csdn.net/detail/qq707548235/9364809
然后需要在主activity中加如下代码
private static final String LAYOUT_LINEARLAYOUT = "LinearLayout";
private static final String LAYOUT_FRAMELAYOUT = "FrameLayout";
private static final String LAYOUT_RELATIVELAYOUT = "RelativeLayout";
在oncreate中加上一句话就行
AutoLayout.getInstance().auto(this);
重写Activity的onCreateView方法
@Override
public View onCreateView(String name, Context context, AttributeSet attrs)
{
View view = null;
if (name.equals(LAYOUT_FRAMELAYOUT))
{
view = new AutoFrameLayout(context, attrs);
}
if (name.equals(LAYOUT_LINEARLAYOUT))
{
view = new AutoLinearLayout(context, attrs);
}
if (name.equals(LAYOUT_RELATIVELAYOUT))
{
view = new AutoRelativeLayout(context, attrs);
}
if (view != null) return view;
return super.onCreateView(name, context, attrs);
}
主方法写这些就可以了,然后要在配置文件中加点东西,在AndroidManifest.xml的application标签中加
<meta-data
android:name="design_width"
android:value="240"></meta-data>
<meta-data
android:name="design_height"
android:value="320"></meta-data>
value值按需更改,和美工设计时采用的分辨率一致,比如上面的意思就是美工是按分辨率240X320的手机为模版进行标注px的
在res/values文件加下的attrs.xml(没有就自己建一个),里面代码如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyDownView">
<attr name="backgroud" format="reference" />
</declare-styleable>
<declare-styleable name="AutoLayout_Layout">
<attr name="layout_auto_textSizeBaseWidth" format="boolean"/>
</declare-styleable>
</resources>
做完这些就可以告一段落,我们在布局文件中有可以安心使用px了,即使是px在不同分辨率的手机上显示比例也是一样的
注意:在studio中可行,eclipse可能还需要额外配置些东西,github上介绍的很详细
https://github.com/hongyangAndroid/AndroidAutoLayout
我在手机上大概试了试,基本是没问题(请原谅我没多次测试,有兴趣的可以各种分辨率都试试)
最后依旧是感谢大家阅读,欢迎大神们指点。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories