2014-05-21 28 views
-2

請考慮代碼給出的代碼。 如何使這些圖像高度爲屏幕尺寸的1/3。如果我用它與適配器。 請參考圖像是否可以設置圖像高度1/3的屏幕尺寸

http://oi59.tinypic.com/fvi7p3.jpg

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="0dip" android:layout_gravity="top" 
     > 


     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"  
      android:paddingBottom="5dp" 
      android:paddingLeft="10dp" 
      android:paddingTop="5dp" > 

      <ImageView 
       android:id="@+id/image" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_gravity="left|top" 
       android:scaleType="centerCrop" 
       android:src="@drawable/ic_launcher" /> 
      <ImageView 
       android:id="@+id/imaget" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left|top" 
       android:layout_weight="1" 
       android:scaleType="centerCrop" 
       android:src="@drawable/ic_launcher" /> 
     </LinearLayout> 

</TableLayout> 

回答

0

你可以得到屏幕的寬度和高度,並設置一個參數兩個 layout_width和按鈕的layout_height。 怎麼樣?

Display display=((WindowManager)getSystemService(Context.WINDOW_SERVICE)). 
             getDefaultDisplay(); 
    int width=display.getWidth(); 
    int height=display.getHeight(); 

//計算的寬度和高度

說對於屏幕240 * 320

buttonWidth=width/5;  result: buttonWidht=48; 
buttonHeight=height/10 result: buttonHeight=32; 

使用的LayoutParams對象,設置寬度和高度的按鈕。

現在,如果你在具有設備的屏幕尺寸320 * 480

buttonWidth=width/5;  result: buttonWidht=64; 
buttonHeight=height/10 result: buttonHeight=48; 

等其他設備也運行這段代碼。

+0

在其表現「的方法getSystemService(字符串)是未定義的類型LazyImageLoadAdapter」 – user3407787

+0

中,你必須通過你的活動範圍內 –

+0

是一個初學者,將ü請告訴我該怎麼做? – user3407787

0

試試這個。

 DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     int density = metrics.densityDpi; 

     int width = 0, height = 0; 
     metrics = new DisplayMetrics();   
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     //height is actual screen height 
     height = metrics.heightPixels;  
     width = metrics.widthPixels; 


     CustomAdapter mAdapter; 
     //pass h to your adapter class and it will gives you the height as per you need 
     h=height/3; 
     mAdapter = new CustomAdapter(this,ImageArrayList1, ImageArrayList2,h); 
相關問題