设计需求:
ViewGroup内有两个View,ViewA, ViewB,需要两个View全部展示在屏幕范围内的ViewGroup中,不允许ViewA的增长会使ViewB挤出屏幕,这就需要ViewB允许拉伸不允许收缩,并且ViewA允被收缩。
过程如下:
当ViewA缩小时,ViewB跟随拉伸:
如图所示:
当ViewA宽度过长,假设9000dp或者更大,ViewB因为不允许收缩,所以ViewB还原到设置width后保持不变,不会被挤出屏幕,ViewA自动收缩
如图所示:
抽象逻辑以后,要实现这种设计需求,就必须满足以上条件,这是如果使用LinerLayout,RelativeLayout,FrameLayout,因为没有拉伸缩放属性设置,很难实现需求。
此需求和Excel表格实现效果十分相似,所以想到TableLayout就可以完美解决问题,于是代码如下:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" //设置第一列允许被拉伸
android:shrinkColumns="0"> //设置滴零列允许收缩
<TableRow>
<Button
android:id="@+id/btn01"
android:layout_width="9000dp"
android:layout_height="200dp"
android:background="@color/starfish_blue_normol"
android:text="允许被收缩\nViewA">
</Button>
<Button
android:id="@+id/btn02"
android:layout_width="120dp"
android:layout_height="200dp"
android:background="@color/red"
android:text="允许被拉伸\nViewB">
</Button>
</TableRow>
</TableLayout>