【发布时间】: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