我想获得一个项目在列表视图中占用的高度。所以我写以下代码: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”返回的值以获得视图的确切高度
返回的高度不是以dps为单位返回,而是以像素为单位返回。你必须做一个转换来获得DPS – Neil