关于Android屏幕适配的一种方法
来源:互联网 发布:每日生产报表软件 编辑:程序博客网 时间:2024/06/04 01:30
Android屏幕适配的方法有很多,记录一下这种屏幕适配方法,这个方法比较费事,需要设置的代码多,优点能适配各种屏幕,在各种屏幕上都能完美适配。
这是公司UI给的样图,样图尺寸为(750*1334),如果直接按上面标注直接写死的话,在小屏幕或者大屏幕上会显示不佳。
解决方法:
public class MainActivity extends AppCompatActivity { ViewPager viewPager; private int screenWidth; private int screenHeight; private int itemHeight; private int itemWidth; private int itemPadding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewPager); getDefault(); } private void getDefault() { //屏幕的宽高 WindowManager wm = this.getWindowManager(); screenWidth = wm.getDefaultDisplay().getWidth(); screenHeight = wm.getDefaultDisplay().getHeight(); //445 为在样图上海报的高度(dp), 667 为样图的高(dp) 计算公式:屏幕上海报的高度/屏幕高度=样图上海报的高度/样图高度 itemHeight = screenHeight * 445 / 667; //宽度计算与高度计算相同 使用screenHeight是要以一边为基准以高度去算它的高度 itemWidth = screenHeight * 250 / 667; //为屏幕上俩边的宽度 itemPadding = (screenWidth - itemWidth) / 2; //设置viewPager的宽高 RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); layoutParams.leftMargin = itemPadding; layoutParams.rightMargin = itemPadding; layoutParams.topMargin = getValue(20); layoutParams.height = itemHeight; viewPager.setLayoutParams(layoutParams); } /** * 1334 为样图整体的高 * itemHeight 为在手机上view的高度 * 公式为 样图上设置的距离/样图整体的高=屏幕上应该设置的高度/在手机上view的高度 * * @param num 样图上设置的距离(px) * @return 屏幕上应该设置的高度 */ private int getValue(int num) { return screenHeight * num / 1334; } }
xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.jam.cardviewpager.MainActivity"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/txt" android:clipChildren="false"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="445dp" android:layout_gravity="bottom" android:clipChildren="false" /> </RelativeLayout></RelativeLayout>
在这个界面上还不能更好的显示出这种适配的效果。只是为了讲原理
阅读全文
0 0
- 关于Android屏幕适配的一种方法
- 一种动态屏幕适配的方法
- Android 屏幕适配的一种方案
- 一种屏幕适配实现方法
- Android 本地HTML屏幕适配的一种方案
- FitScreen-Android屏幕适配的一种方案
- 截取屏幕的一种方法
- 关于Android的多种屏幕适配
- 关于Android的多种屏幕适配
- 关于Android的多种屏幕适配
- 关于android屏幕适配的问题
- 关于android的屏幕适配
- 关于Android屏幕适配的问题
- 关于android屏幕适配的解决方案
- android 关于屏幕适配
- 关于 android屏幕适配
- 关于Android屏幕适配
- Android 关于屏幕适配
- 【转】Makefile的内嵌函数
- JavaScript面向对象方式实现栈结构的字符串逆序
- finally 块中抛出异常该如何处理
- 实现浏览器兼容版的element.dataset
- 离散题目2
- 关于Android屏幕适配的一种方法
- java中的集合
- 学习vue的一些资源
- Fragment的setUserVisibleHint方法实现懒加载
- Exception Value: no such table: pizzas_topping
- Java 中的集合
- clipse上安装GIT插件EGit及使用
- AfxGetApp()->m_pMainWnd和AfxGetMainWnd()
- Android:读取Word文档