zoukankan      html  css  js  c++  java
  • Android屏幕适配和文字屏幕适配

    http://blog.sina.com.cn/s/blog_9996c67e0101euwd.html

    最近在一个项目中要实现屏幕适配平板和手机等不同的型号,而蛋疼的美工给了一套图,而且这些图纸有在1280×800的平板上才能布局正常,所以没办法,只能自己一步一步的写了,终于让俺在度娘和谷哥上找到了类似的解决办法,现将我自己实现的方法粘贴出来:
    首先俺有一个activity_index.xml文件:
        <xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/ergetiandi_back1" >
     
       
            android:id="@+id/rel_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" >
     
           
                android:id="@+id/er_ge_tian_di"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="60dip"
                android:layout_marginTop="80dip"
                android:background="@drawable/select_go_back_press" />
     
           
                android:id="@+id/upper_paper"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="620dip"
                android:layout_marginTop="50dip"
                android:background="@drawable/select_pager_up_press" />
     
           
                android:id="@+id/next_paper"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="620dip"
                android:layout_marginTop="50dip"
                android:background="@drawable/select_pager_down_press" />
     
           
                android:id="@+id/song"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="160dip"
                android:layout_marginTop="160dip"
                android:background="@drawable/select_song_press" />
     
           
                android:id="@+id/accompany"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/song"
                android:layout_marginLeft="21dp"
                android:layout_toRightOf="@+id/song"
                android:background="@drawable/select_accompany_press"
                />
       
       
            android:id="@+id/gridView1"
            android:layout_width="fill_parent"
            android:layout_height="450dip"
            android:layout_below="@+id/rel_title"
            android:layout_marginTop="60dip"
            android:numColumns="5"
             >
        <xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/ergetiandi_back1" >
     
       
            android:id="@+id/rel_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" >
     
           
                android:id="@+id/er_ge_tian_di"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="60dip"
                android:layout_marginTop="80dip"
                android:background="@drawable/select_go_back_press" />
     
           
                android:id="@+id/upper_paper"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="620dip"
                android:layout_marginTop="50dip"
                android:background="@drawable/select_pager_up_press" />
     
           
                android:id="@+id/next_paper"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="620dip"
                android:layout_marginTop="50dip"
                android:background="@drawable/select_pager_down_press" />
     
           
                android:id="@+id/song"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="160dip"
                android:layout_marginTop="160dip"
                android:background="@drawable/select_song_press" />
     
           
                android:id="@+id/accompany"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/song"
                android:layout_marginLeft="21dp"
                android:layout_toRightOf="@+id/song"
                android:background="@drawable/select_accompany_press"
                />
       
       
            android:id="@+id/gridView1"
            android:layout_width="fill_parent"
            android:layout_height="450dip"
            android:layout_below="@+id/rel_title"
            android:layout_marginTop="60dip"
            android:numColumns="5"
             >
       
    然后在类文件中对应的填写xml对应的值就行:
    private void getFindViews() {
    upper_paper = (Button) findViewById(R.id.upper_paper);
    next_paper = (Button) findViewById(R.id.next_paper);
    btnBack = (Button) this.findViewById(R.id.er_ge_tian_di);  
    btnSong = (Button) this.findViewById(R.id.song);
    btnAccompany = (Button) this.findViewById(R.id.accompany);
     
    RelativeLayout.LayoutParams er_ge_tian_diParams = new RelativeLayout.LayoutParams((int) (60 * Config.WITH_RATE), (int) (60 * Config.HEIGHT_RATE));
    er_ge_tian_diParams.leftMargin = (int)(60 * Config.WITH_RATE);
    er_ge_tian_diParams.topMargin = (int)(80 * Config.HEIGHT_RATE);
    btnBack.setLayoutParams(er_ge_tian_diParams);
     
     
     
    RelativeLayout.LayoutParams next_paperParams=new RelativeLayout.LayoutParams((int) (130 * Config.WITH_RATE), (int) (55 * Config.HEIGHT_RATE));
    next_paperParams.leftMargin=(int)(620 * Config.WITH_RATE);
    next_paperParams.topMargin=(int)(50 * Config.HEIGHT_RATE);
    next_paper.setLayoutParams(next_paperParams);
     
     
    RelativeLayout.LayoutParams upper_paperParams=new RelativeLayout.LayoutParams((int) (130 * Config.WITH_RATE), (int) (55 * Config.HEIGHT_RATE));
    upper_paperParams.leftMargin=(int)(620 * Config.WITH_RATE);
    upper_paperParams.topMargin=(int)(50 * Config.HEIGHT_RATE);
    upper_paper.setLayoutParams(upper_paperParams);
     
     
    RelativeLayout.LayoutParams songParams=new RelativeLayout.LayoutParams((int) (60 * Config.WITH_RATE), (int) (30 * Config.HEIGHT_RATE));
    songParams.leftMargin=(int)(160 * Config.WITH_RATE);
    songParams.topMargin=(int)(160 * Config.HEIGHT_RATE);
    btnSong.setLayoutParams(songParams);
     
     
    RelativeLayout.LayoutParams accompanyParams=new RelativeLayout.LayoutParams((int) (60 * Config.WITH_RATE), (int) (30 * Config.HEIGHT_RATE));
    accompanyParams.leftMargin=(int)(21 * Config.WITH_RATE);
    accompanyParams.addRule(RelativeLayout.RIGHT_OF, R.id.song);
    accompanyParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.song);
    btnAccompany.setLayoutParams(accompanyParams);
     
     
    RelativeLayout.LayoutParams gridView1Params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, (int) (450 * Config.HEIGHT_RATE));
    gridView1Params.topMargin=(int)(60 * Config.HEIGHT_RATE);
    gridView1Params.addRule(RelativeLayout.BELOW, R.id.rel_title);
    mGridView.setLayoutParams(gridView1Params);
    private void getFindViews() {
    upper_paper = (Button) findViewById(R.id.upper_paper);
    next_paper = (Button) findViewById(R.id.next_paper);
    btnBack = (Button) this.findViewById(R.id.er_ge_tian_di);  
    btnSong = (Button) this.findViewById(R.id.song);
    btnAccompany = (Button) this.findViewById(R.id.accompany);
     
    RelativeLayout.LayoutParams er_ge_tian_diParams = new RelativeLayout.LayoutParams((int) (60 * Config.WITH_RATE), (int) (60 * Config.HEIGHT_RATE));
    er_ge_tian_diParams.leftMargin = (int)(60 * Config.WITH_RATE);
    er_ge_tian_diParams.topMargin = (int)(80 * Config.HEIGHT_RATE);
    btnBack.setLayoutParams(er_ge_tian_diParams);
     
     
     
    RelativeLayout.LayoutParams next_paperParams=new RelativeLayout.LayoutParams((int) (130 * Config.WITH_RATE), (int) (55 * Config.HEIGHT_RATE));
    next_paperParams.leftMargin=(int)(620 * Config.WITH_RATE);
    next_paperParams.topMargin=(int)(50 * Config.HEIGHT_RATE);
    next_paper.setLayoutParams(next_paperParams);
     
     
    RelativeLayout.LayoutParams upper_paperParams=new RelativeLayout.LayoutParams((int) (130 * Config.WITH_RATE), (int) (55 * Config.HEIGHT_RATE));
    upper_paperParams.leftMargin=(int)(620 * Config.WITH_RATE);
    upper_paperParams.topMargin=(int)(50 * Config.HEIGHT_RATE);
    upper_paper.setLayoutParams(upper_paperParams);
     
     
    RelativeLayout.LayoutParams songParams=new RelativeLayout.LayoutParams((int) (60 * Config.WITH_RATE), (int) (30 * Config.HEIGHT_RATE));
    songParams.leftMargin=(int)(160 * Config.WITH_RATE);
    songParams.topMargin=(int)(160 * Config.HEIGHT_RATE);
    btnSong.setLayoutParams(songParams);
     
     
    RelativeLayout.LayoutParams accompanyParams=new RelativeLayout.LayoutParams((int) (60 * Config.WITH_RATE), (int) (30 * Config.HEIGHT_RATE));
    accompanyParams.leftMargin=(int)(21 * Config.WITH_RATE);
    accompanyParams.addRule(RelativeLayout.RIGHT_OF, R.id.song);
    accompanyParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.song);
    btnAccompany.setLayoutParams(accompanyParams);
     
     
    RelativeLayout.LayoutParams gridView1Params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, (int) (450 * Config.HEIGHT_RATE));
    gridView1Params.topMargin=(int)(60 * Config.HEIGHT_RATE);
    gridView1Params.addRule(RelativeLayout.BELOW, R.id.rel_title);
    mGridView.setLayoutParams(gridView1Params);
  • 相关阅读:
    记好这24个ES6方法,用于解决实际开发的JS问题
    es6 扩展运算符 剩余运算符 ...
    Django基础006--在pycharm中将项目配置为Django项目
    Django基础005-Django开发的整体过程
    Django基础-004 上下文管理器&中间件&前端公共代码复用
    Django基础-003 配置Django自带的后台管理,操作数据库
    Django基础-002 Models的属性与字段
    jconsole和jstack
    Django基础-001
    前端009-vue框架
  • 原文地址:https://www.cnblogs.com/niray/p/3815054.html
Copyright © 2011-2022 走看看