首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

LinearLayout 2视图,尽管我更新了weightSum,但如果一个视图消失了,它不会将另一个视图居中

LinearLayout是Android中常用的布局容器,用于在垂直或水平方向上排列子视图。weightSum是LinearLayout的一个属性,用于设置子视图的权重总和。当一个视图消失时,如果没有设置居中的属性,LinearLayout不会自动将另一个视图居中。

要实现将另一个视图居中,可以使用LinearLayout的gravity属性。gravity属性用于设置子视图的对齐方式,包括居中对齐、左对齐、右对齐等。在这种情况下,可以将gravity属性设置为居中对齐,以实现将另一个视图居中。

以下是一个示例代码:

代码语言:txt
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF0000" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#00FF00"
        android:visibility="gone"
        android:gravity="center" />

</LinearLayout>

在上述示例中,LinearLayout的weightSum属性设置为2,表示子视图的权重总和为2。第一个视图的权重为1,第二个视图的权重也为1。第二个视图的visibility属性设置为gone,表示该视图消失。同时,第二个视图的gravity属性设置为center,表示将该视图居中对齐。

这样,当第二个视图消失时,第一个视图会自动居中显示。

推荐的腾讯云相关产品和产品介绍链接地址:

  • 腾讯云移动开发平台:https://cloud.tencent.com/product/mmp
  • 腾讯云云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 腾讯云云原生应用引擎(TKE):https://cloud.tencent.com/product/tke
  • 腾讯云数据库(TencentDB):https://cloud.tencent.com/product/cdb
  • 腾讯云人工智能(AI):https://cloud.tencent.com/product/ai
  • 腾讯云物联网(IoT):https://cloud.tencent.com/product/iotexplorer
  • 腾讯云存储(COS):https://cloud.tencent.com/product/cos
  • 腾讯云区块链(BCS):https://cloud.tencent.com/product/bcs
  • 腾讯云视频处理(VOD):https://cloud.tencent.com/product/vod
  • 腾讯云音视频通信(TRTC):https://cloud.tencent.com/product/trtc
  • 腾讯云网络安全(SSL):https://cloud.tencent.com/product/ssl
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券