【问题标题】:LinearLayout items display and orientation changeLinearLayout 项目显示和方向更改
【发布时间】:2017-10-26 13:28:18
【问题描述】:

我想问是否有任何可能的方法使线性布局显示项目,如 CSS 中的 display: inline-flex?

我的意思是当我改变设备的方向时,我想(在水平模式下)内联显示项目(同样当一个项目的可见性设置为消失时,我想按百分比更改其他项目的宽度,例如 3 个项目 - 每个 30%项,2 项 - 50%,一项 100%)。

在垂直模式下,项目应该一个接一个地显示。

顺便说一句 - 如何防止在以编程方式更改视图时重置视图? (我有问题,因为当我用文本填充输入框时 - 当我更改设备方向等时,文本会消失。

【问题讨论】:

  • 查看layout_weightLinearLayout 中子项的属性:developer.android.com/guide/topics/ui/layout/linear.html
  • layout_weight 不符合我的期望或可能符合,但我想同时知道如何防止视图重置。
  • 您不能“阻止视图重置”(除非您完全阻止方向更改),但您可以通过覆盖 Activity 的 onSaveInstanceState(Bundle) 来保存数据状态。当方向改变时,系统会自动调用它,然后在onCreate(Bundle) 中将Bundle 传回给你。从那里您可以检查Bundle 是否为空并做出相应的响应。
  • 可以肯定 - 如果我改变方向,动画元素也会重置他的动画键?
  • 正确,除非您使用onSavedInstance 设置标志,并在运行动画之前检查onCreate 中的该标志

标签: android xml layout orientation


【解决方案1】:
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

当你隐藏视图时,你必须使用可见性消失,然后它会占用相等的空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多