关于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>

在这个界面上还不能更好的显示出这种适配的效果。只是为了讲原理

原创粉丝点击