Android动态屏幕拆分

时间:2015-04-17 06:19:58

标签: android android-layout android-layout-weight

我想做什么?

活动以ImageView占据屏幕上方9/10开始,ListView剩余底部1/10开始: enter image description here

随着更多项目添加到ListView,比率会发生变化,ListView每个项目的屏幕数增加1/10,ImageView分别缩小,最多50:50比率(在此之后添加更多项目保持比率固定): enter image description here

我已经知道的事情: 静态划分屏幕的最佳做法是使用LinearLayout属性android:weightSum(和View的{​​{1}})。它应该可以使用android:layout_weight动态地结合在AbsListView.LayoutParams中的某处注册回调。然而,它像滥用静态功能(重量)一样填充。此外,我更愿意连续进行比率转换(使用属性动画)。

问题:

  1. 我是否应该优先使用ArrayAdapter一次,并根据需要动态计算比率?

  2. 对于比率更新回调,除了getWindowManager() .getDefaultDisplay().getHieght();之外还有其他选项吗?

2 个答案:

答案 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 );