Android-带有级别指示器标志和文本输入/输出进度的进度条

Android-带有级别指示器标志和文本输入/输出进度的进度条,android,android-progressbar,Android,Android Progressbar,我在这里遇到了一个非常困难的问题,我已经尽了我所能找到的一切,但仍然一事无成 我需要这样的东西: 一个progressBar,其中有一个包含进度数据的文本,如果该文本不适合,则该文本可以在进度级别之外,如果适合,则该文本可以在进度级别之内(获取它)。 还有一个进度百分比标志,正好位于进度级别(真正的问题)之下 样本: 有关更多示例,请将“orange.png”更改为“red.png”和“green.png” 我是这样做的: 为progressBar中的文本添加TextView: <mer

我在这里遇到了一个非常困难的问题,我已经尽了我所能找到的一切,但仍然一事无成

我需要这样的东西:

一个
progressBar
,其中有一个包含进度数据的文本,如果该文本不适合,则该文本可以在进度级别之外,如果适合,则该文本可以在进度级别之内(获取它)。 还有一个进度百分比标志,正好位于进度级别(真正的问题)之下

样本:

有关更多示例,请将“orange.png”更改为“red.png”和“green.png”

我是这样做的:

progressBar
中的文本添加
TextView

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

<RelativeLayout
    android:id="@+id/progressBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp" >

    <ImageView
        android:id="@+id/track_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/app_name"
        android:minHeight="25dp" />

    <ImageView
        android:id="@+id/progress_drawable_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:contentDescription="@string/app_name"
        android:minHeight="25dp" />

    <TextView
        android:id="@+id/progress_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textColor="#FFF"
        android:textSize="14sp"
        android:textStyle="bold" />
</RelativeLayout>

对于设置文本输入/输出进度,我这样做:

double percent = (double) value / (double) max;
int level = (int) Math.floor(percent * 10000);
drawable.setLevel(level);
double pixelLevel = percent * progressDrawableImageView.getWidth();
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) txt
            .getLayoutParams();

    if (pixelLevel - 20 < bounds.width()) {
        params.leftMargin = (int) pixelLevel + 20;
    } else {
        params.leftMargin = (int) pixelLevel - bounds.width() - 10;
    }

    txt.setLayoutParams(params);
double percent=(double)value/(double)max;
整数级=(整数)数学下限(百分比*10000);
可拉深。设置标高(标高);
double pixelLevel=percent*progressDrawableImageView.getWidth();
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)txt
.getLayoutParams();
如果(像素级别-20
这很好,但不适用于百分比标志。或者至少不适用于所有屏幕尺寸和密度

项目:“相同的dropbox链接”/RoundedProgressSampleCustom.zip

谢谢你帮助我


PS:很抱歉不能发布超过2个链接:c

潜在问题1

很难确定你的实际问题是什么。我假设您的百分比标志在所有屏幕上的位置都不正确。我注意到您在XML中使用DP进行测量,而在代码中使用PX调整参数

尝试使用DP进行代码中的测量(如20和像素级)。您可以使用以下命令

int valueInDP= (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, YOURVALUE, getResources().getDisplayMetrics());
其中,YOURVALUE是您希望转换为DP的值


潜在问题2

如果你什么都没看到,你的问题可能就在这条线上

progressDrawableImageView.getWidth();
由于在onCreate()中获取imageView宽度时出现问题。这可能与你有关,也可能与你无关,但很难说清楚。请参阅此链接了解解决方案:


如果定位不是问题所在,您必须编辑您的问题,使其更加具体。

谢谢!它通过
int-valueInDP=(int)TypedValue.applyDimension(TypedValue.COMPLEX\u UNIT\u DIP,YOURVALUE,getResources().getDisplayMetrics())解决