2016-12-09 22 views
0

我想获得一个项目在列表视图中占用的高度。所以我写以下代码:getHeight返回dp单元中的值吗?

mLisVieMeineDocs.post(new Runnable() { 
     @Override 
     public void run() { 
      h = mLisVieMeineDocs.getHeight(); 
      Log.d(TAG, "getHeight :" + h);//returns 108 

      FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mLisVieMeineDocs.getLayoutParams(); 
      params.width = LinearLayout.LayoutParams.MATCH_PARENT; 
      params.height = h; 
      mLisVieMeineDocs.setLayoutParams(params); 
     } 
    }); 

在代码中所述,“的getHeight”返回108.当我试图设置在列表视图的容器的layout_height该值“108”中的XML文件如下:

<LinearLayout 
      android:id="@+id/versicherungsListeActivity2mod_linLay_meineDocList_container" 
      android:layout_width="match_parent" 
      android:layout_height="108dp" <<<------ 
      android:orientation="vertical" 
      android:layout_below="@+id/versicherungslistsactivity2mod_linLay_meineDocsBar_container"> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:fillViewport="true"> 

       <!--android:transcriptMode="alwaysScroll" 
        android:stackFromBottom="true"--> 
       <ListView 
        android:id="@+id/versicherungsListeActivity2mod_lisVie_meineDocs" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"></ListView> 
      </ScrollView> 
     </LinearLayout> 

我发现的LinearLayout扩展到包裹高达在ListView 2项despit上面的代码表示,该项目在列表视图的高度为108 但是,当我改变108到54dp在xml文件中

android:layout_height="54dp" 

然后linearlayout扩展到在列表视图中的项目上完全包装。

那么,为什么“getHeight”返回视图高度的两倍?为什么我需要分割从“getHeight”返回的值以获得视图的确切高度

+2

返回的高度不是以dps为单位返回,而是以像素为单位返回。你必须做一个转换来获得DPS – Neil

回答

0

您需要将像素转换为dp,请参阅下面的代码。

h = pxToDp(mLisVieMeineDocs.getHeight()); 

public static int pxToDp(int px) { 
    return (int) (px/Resources.getSystem().getDisplayMetrics().density); 
} 

public static int dpToPx(int dp) { 
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density); 
} 
0

Java中的大部分值为Pixel,所以您需要转换它。

public static int dip2px(Context context, float dp) { 
    float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (dp * scale + 0.5f); 
} 

public static int px2dip(Context context, float px) { 
    float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (px/scale + 0.5f); 
} 

有时你应该设置DP/SP值,例如: badgeCountTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);

TypedValue.COMPLEX_UNIT_DIP, TypedValue.COMPLEX_UNIT_PX....

0

转换从DP到像素以下方法使用:

public static float convertDpToPixel(float dp, Context context){ 
    Resources resources = context.getResources(); 
    DisplayMetrics metrics = resources.getDisplayMetrics(); 
    float px = dp * ((float)metrics.densityDpi/DisplayMetrics.DENSITY_DEFAULT); 
    return px; 
} 

从像素转换以DP方式使用以下方法:

public static float convertPixelsToDp(float px, Context context){ 
    Resources resources = context.getResources(); 
    DisplayMetrics metrics = resources.getDisplayMetrics(); 
    float dp = px/((float)metrics.densityDpi/DisplayMetrics.DENSITY_DEFAULT); 
    return dp; 
} 
相关问题