android - 如何制作包含多个部分的单杠?

标签 android graphics

如何在 Android 中创建一个包含多个部分的栏?我试过 ProgressBar,但我无法让它工作,而且听起来也不是 ProgressBar 的正确应用。在 HTML 中,我只使用类似的东西:

<style>
#bar { width: 100px; }
.bar { height: 20px; }
.black { background-color: black; }
</style>
<div id="bar">
    <div class="bar"       style="width: 15px;"></div>
    <div class="bar black" style="width: 25%;"></div>
    <div class="bar"       style="width: 5%;"></div>
    <div class="bar black" style="width: 40%;"></div>
    <div class="bar"       style="width: 15%;"></div>
</div>

Android 中的高效等价物是什么?

bar

最佳答案

使用水平 LinearLayout .
然后,您可以在其中创建 View 并使用 layout_weight 属性决定它们的相对大小。

这是一个给出你想要的结果的例子:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="100" >
    <View
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="15"
        android:background="#FFFF" />
    <View
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="25"
        android:background="#F000" />

    <View
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="5"
        android:background="#FFFF" />

    <View
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="40"
        android:background="#F000" />
    <View
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="15"
        android:background="#FFFF" />
</LinearLayout>

请注意,权重是 float 。我只是将整数和 100 的总和与您基于百分比的布局相对应。

关于android - 如何制作包含多个部分的单杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10028813/

相关文章:

android - RxAndroid - 点击重试观察

android - 具有DiffUtil和LiveData的RecyclerView

java - 在Java中调用位图类

javascript - 如何在网站的菜单页面上添加 3d Logo ?

java - 很难理解 Swing 图形...以及如何在运行时绘制

graphics - 从 R 中的一系列情节创建电影

android - 如何在android中为按钮添加图像?

android - Google Search API for Shopping for Android 应用程序的替代品

android - LocalBroadcastManager 只有支持库吗?

c# - System.Drawing.dll 中发生类型为 'System.ArgumentException' 的第一次机会异常