【问题标题】:how to call different views according to a recived state如何根据接收到的状态调用不同的视图
【发布时间】:2015-09-22 11:59:28
【问题描述】:

我有一个可展开的列表视图,父“组”行包含“用于名称”的 TextView 和“用于 RSSI”的 ProgressBar。现在的 RSSI 代表 信号健康度可以是 20%、40%、60%、80% 或 100%,进度条应指示信号的健康度。

所以我创建了 5 个进度条,其值 android:progress="" 根据信号强度而有所不同。

举个例子,

if the RSSI = -70; I should display the progressBar with 20%

if the RSSI = -50; I should display the progressBar with 40%

if the RSSI = -30; I should display the progressBar with 60%

if the RSSI = -10; I should display the progressBar with 80%

所以我创建了 belwo xml 文件,该文件显示具有不同进度百分比的进度条。

现在我的问题是,根据收到的 RSSI 调用每个进度条的推荐方式是什么

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:paddingTop="10dp"
tools:context=".ProgressBarTutorial" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:progressDrawable="@drawable/progress"
    android:progress="20" />

<ProgressBar
    android:id="@+id/progressBar2"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:progressDrawable="@drawable/progress"
    android:layout_below="@+id/progressBar1"
    android:layout_marginTop="30dp"
    android:progress="40"/>

<ProgressBar
    android:id="@+id/progressBar3"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:progressDrawable="@drawable/progress"
    android:layout_below="@+id/progressBar2"
    android:layout_marginTop="30dp"
    android:progress="60"/>

<ProgressBar
    android:id="@+id/progressBar4"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:progressDrawable="@drawable/progress"
    android:layout_below="@+id/progressBar3"
    android:layout_marginTop="30dp"
    android:progress="80"/>

<ProgressBar
    android:id="@+id/progressBar5"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:progressDrawable="@drawable/progress"
    android:layout_below="@+id/progressBar4"
    android:layout_marginTop="30dp"
    android:progress="100"/>

</RelativeLayout>

【问题讨论】:

    标签: android progress-bar views android-progressbar


    【解决方案1】:

    我不明白,为什么你创建了 5 个进度条来显示进度,但是,如果你想显示进度条,你应该使用 mProgressBar.setVisibility(View.Visible); 和隐藏使用 mProgressBar.setVisibility(View.GONE);

    View.setVisibility(int)

    对于progressBars的访问,在我看来,你可以使用Map类的一些实现。 (例如HashMap

    如果您只想显示进度,最好的解决方案是使用ProgressBar.setProgress(int)

    【讨论】:

      【解决方案2】:

      如果您想在代码中动态添加/显示进度条,您可以使用set visibility(int VISIBILITY) 方法和常量View.VISIBLEView.INVISIBLE

      但是,我认为最好只有一个操作栏并根据您的 RSSI 更改进度(在您的进度条上使用 setProgress(int))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-05
        • 2012-01-27
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-01
        相关资源
        最近更新 更多