【问题标题】:how to set scaleble (weighted) space between linearlayout children?如何在线性布局子级之间设置可缩放(加权)空间?
【发布时间】:2013-07-28 11:39:47
【问题描述】:

我想在 linearLayout 的两个子级之间创建一个间隙。

我不想使用边距或填充,因为这将无法正确缩放

当我切换到横向时。

我更喜欢使用重量。但是,如何在它们之间创建一个虚拟(仅间隙)视图?

顺便说一句,如果我不创建横向 xml 会发生什么

将设备方向从纵向更改为横向?

更新 1

我试过了:

<LinearLayout
    android:id="@+id/layout_information"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="0.47"
    android:orientation="vertical" 
    android:gravity="center"
    >

    <TextView
        android:id="@+id/text_view_destination"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="\@ Home in"
        android:textColor="@color/solid_white"
        android:textSize="19sp"
        android:textStyle="bold" />

    <View
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"/>

    <TextView
        android:id="@+id/text_view_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="40dp"
        android:textColor="@color/solid_white"
        android:textSize="25sp"
        android:textStyle="normal" />
</LinearLayout>

然后

我收到有关嵌套加权性能的警告。

绘制小工具时出现运行时错误

【问题讨论】:

  • 尝试用 0dp 替换你的“wrap_content”高度.. ;-)

标签: java android xml android-layout layout


【解决方案1】:

如果您正在为 ICS 及更高版本构建,请使用 Space 小部件:

<Space
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="YourWeight"/>

(http://developer.android.com/reference/android/widget/Space.html)

否则,您可以只使用一个空的View

您还可以考虑在您的情况下使用RelativeLayout 是否更有效。

当设备旋转并且您没有专门的横向布局时,将使用您的纵向。

【讨论】:

  • 为什么RelativeLayout效率更高?我仍然需要创建空视图
  • 如果有其他Views 可以用作Views 的锚点,则不必创建空视图。但是,我不知道您的确切布局结构,所以这只是一个建议。
  • 我认为 match_parentweight 属性冲突
  • 只要您的所有其他视图有weightmatch_parentfill_parentweight 就不会冲突。
猜你喜欢
  • 2021-08-19
  • 2018-06-03
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-31
相关资源
最近更新 更多