Android屏幕適配和文字屏幕適配

最近在一個項目中要實現屏幕適配平板和手機等不一樣的型號,而蛋疼的美工給了一套圖,並且這些圖紙有在1280×800的平板上才能佈局正常,因此沒辦法,只能本身一步一步的寫了,終於讓俺在度娘和谷哥上找到了相似的解決辦法,現將我本身實現的方法粘貼出來:

首先俺有一個activity_index.xml文件: java

<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);
		
		upper_paper.setOnClickListener(this);   
		next_paper.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		btnSong.setOnClickListener(this);
		btnAccompany.setOnClickListener(this);
		checkButton();
	}
相關文章
相關標籤/搜索