我想做什么?
活动以ImageView
占据屏幕上方9/10开始,ListView
剩余底部1/10开始:
随着更多项目添加到ListView
,比率会发生变化,ListView
每个项目的屏幕数增加1/10,ImageView
分别缩小,最多50:50比率(在此之后添加更多项目保持比率固定):
我已经知道的事情:
静态划分屏幕的最佳做法是使用LinearLayout
属性android:weightSum
(和View
的{{1}})。它应该可以使用android:layout_weight
动态地结合在AbsListView.LayoutParams
中的某处注册回调。然而,它像滥用静态功能(重量)一样填充。此外,我更愿意连续进行比率转换(使用属性动画)。
问题:
我是否应该优先使用ArrayAdapter
一次,并根据需要动态计算比率?
对于比率更新回调,除了getWindowManager() .getDefaultDisplay().getHieght();
之外还有其他选项吗?
答案 0 :(得分:2)
如果您想保持比率始终为10%和90%,请使用重量1和重量9。 否则最好写xml代码来检查
答案 1 :(得分:1)
我做了你想做的事情:
1)设置布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_forImage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_forList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</LinearLayout>
2)编写一些实用的布局方法。
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}
private static void setLayoutSize(View view, int width, int height) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
3)代码:
int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
int screenHeight = dpToPx(getResources().getConfiguration().screenHeightDp);
int ratio = ....;//
setLayoutSize(ll_forImage, screenWidth , screenHeight/ratio );
setLayoutSize(ll_forList, screenWidth , screenHeight - screenHeight/ratio );