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);
Android屏幕适配和文字屏幕适配
时间: 2024-10-30 05:33:11