【问题标题】:How to divide space equally among odd number of views in a layout using support percent library?如何使用支持百分比库在布局中的奇数个视图之间平均分配空间?
【发布时间】:2015-10-29 12:44:54
【问题描述】:

我在我的项目中使用支持百分比库。

compile 'com.android.support:percent:23.1.0'

我在 LinearLayout 中有三个 ImageView,它们应该平均占用空间(在水平方向上)。但是由于百分比支持库只有 PercentRelativeLayout 和 PercentFrameLayout 所以我搜索了 PercentLinearLayout 并找到了one sample for it

这是布局代码:

<com.example.layouts.PercentLinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/img_01"
        app:layout_widthPercent="33.3%" />  // <- what should I write here?

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/img_02"
        app:layout_widthPercent="33.3%" />  // <- and here..

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/img_03"
        app:layout_widthPercent="33.3%" /> // <- and here..

</com.example.layouts.PercentLinearLayout>

您可以看到我为每个 ImageView 分配了 33.3% 的空间,但还剩下 0.1% 的空间。虽然 0.1% 的屏幕空间并不重要,但目前的方法对我来说似乎并不合适。

PS:之前我使用权重(每个 ImageView 的权重为 1),但由于它不利于性能,所以我决定改用百分比。

【问题讨论】:

  • 您可以在不使用支持库的情况下实现这一目标,而是使用带有权重的线性布局并将您的图像视图嵌套在线性布局中。
  • 达到 33.333%,那么只剩下 0.001%。
  • “之前我使用权重(每个 ImageView 的权重为 1),但由于它不利于性能”——请发布您的性能分析,表明您对 LinearLayout 和相等权重的具体使用是“对性能不好”,无论是绝对基础还是与您在此处尝试的相比。 所有布局都以某种方式“不利于性能”。
  • @activesince93:这并不能真正回答问题。
  • @CommonsWare:好的。假设我在另一个布局中有这个 PercentLinearLayout 并为其分配了一些权重。那么,这不会被认为是嵌套权重的情况,我认为这对性能不利,对吧?

标签: android android-layout android-support-library android-percent-library percent-support-library


【解决方案1】:

您可以使用 LinearLayout 并将 weightSum 设置为 3 并将 android:layout_weight="1" 设置为每个 ImageView

【讨论】:

  • 这正是我之前所做的。我想知道如何使用百分比支持库来处理这种情况。
【解决方案2】:

如果你想用百分比来做,你可以将 weightSum 设置为 100,然后将 android:layout_weight 设置为你想要的百分比数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多