MVVMCross本机Android Progressbar使用当前进度移动元素
我有一个进度条,如下图所示。我的问题是,我想移动显示当前进度步骤的文本视图以及进度 因此,在步骤3/7中,它应该随着进度而移动。内部可绘制是一个样式化的xml形状可绘制 圆角\u进度\u条MVVMCross本机Android Progressbar使用当前进度移动元素,android,get,progress-bar,drawable,Android,Get,Progress Bar,Drawable,我有一个进度条,如下图所示。我的问题是,我想移动显示当前进度步骤的文本视图以及进度 因此,在步骤3/7中,它应该随着进度而移动。内部可绘制是一个样式化的xml形状可绘制 圆角\u进度\u条 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@androi
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="8dp"/>
<solid android:color="@color/progressbarBgColor"/>
</shape>
</item>
<item android:id="@android:id/progress"
android:top="1dp"
android:bottom="1dp"
android:left="1dp"
android:right="1dp">
<scale android:scaleWidth="100%">
<shape>
<corners android:radius="8dp"/>
<solid android:color="?attr/colorAccent"/>
</shape>
</scale>
</item>
</layer-list>
总体布局(这是一个MVVMCross原生Android项目,因此本地:MvxBind与此相关)
我建议借助约束布局,它为您提供了最佳的实现机制 首先,创建一个约束布局,将进度条放在其中,并将文本视图放在其中,您希望在其中设置文本量过程,并根据过程百分比移动偏差,如下图所示 布局
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:id="@+id/mConstraintLayout"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="match_parent"
android:id="@+id/mProgressBar"
android:layout_height="wrap_content" />
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:id="@+id/mTVValue"
android:text="7/10"
app:layout_constraintStart_toStartOf="@+id/mProgressBar"
app:layout_constraintEnd_toEndOf="@+id/mProgressBar"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>
根据上面的观点,它就像
ConstraintLayout constraintLayout=findViewById(R.id.mConstraintLayout);
moveProgressPercent(constraintLayout,R.id.mTVValue,25);
这是我的解决方案。
我创建了一个类,并使用RelativeLayout
对其进行了扩展,扩展了布局(您可以在上面的问题中看到)
添加了侦听器以侦听布局更改\u progressBar.ViewTreeObserver.GlobalLayout+=ViewTreeObserver\u GlobalLayout代码>
private void ViewTreeObserver_GlobalLayout(object sender, EventArgs e)
{
_progressBar.ViewTreeObserver.GlobalLayout -= ViewTreeObserver_GlobalLayout;
SetProgressTextPosition();
}
然后我只计算当前进度,并相应地设置x
//_progressBar = FindViewById<Android.Widget.ProgressBar>(Resource.Id.progressBar);
//_currentProgressStep = FindViewById<TextView>(Resource.Id.currentProgressStep);
private void SetProgressTextPosition()
{
if (!TotalStepCount.HasValue || !CurrentStep.HasValue)
{
return;
}
if (CurrentStep.Value > TotalStepCount.Value)
{
throw new ArgumentOutOfRangeException($"Please ensure that the current step does not exceed the total step amount.");
}
_currentProgressStep.Text = $"{CurrentStep.Value} / {TotalStepCount.Value}";
float startPosition = _progressBar.GetX();
float totalWidth = startPosition + _progressBar.Width;
float stepSize = totalWidth / TotalStepCount.Value;
float currentPosition = stepSize * CurrentStep.Value;
_currentProgressStep.SetX(currentPosition - _currentProgressStep.Width);
}
/\u progressBar=findviewbyd(Resource.Id.progressBar);
//_currentProgressStep=FindViewById(Resource.Id.currentProgressStep);
私有void SetProgressTextPosition()
{
如果(!TotalStepCount.HasValue | |!CurrentStep.HasValue)
{
返回;
}
如果(CurrentStep.Value>TotalStepCount.Value)
{
抛出新ArgumentOutOfRangeException($“请确保当前步骤不超过步骤总数。”);
}
_currentProgressStep.Text=$“{CurrentStep.Value}/{TotalStepCount.Value}”;
float startPosition=_progressBar.GetX();
float totalWidth=startPosition+_progressBar.Width;
浮动步长=总宽度/TotalStepCount.Value;
float currentPosition=步长*当前步长值;
_currentProgressStep.SetX(currentPosition-_currentProgressStep.Width);
}
private void ViewTreeObserver_GlobalLayout(object sender, EventArgs e)
{
_progressBar.ViewTreeObserver.GlobalLayout -= ViewTreeObserver_GlobalLayout;
SetProgressTextPosition();
}
//_progressBar = FindViewById<Android.Widget.ProgressBar>(Resource.Id.progressBar);
//_currentProgressStep = FindViewById<TextView>(Resource.Id.currentProgressStep);
private void SetProgressTextPosition()
{
if (!TotalStepCount.HasValue || !CurrentStep.HasValue)
{
return;
}
if (CurrentStep.Value > TotalStepCount.Value)
{
throw new ArgumentOutOfRangeException($"Please ensure that the current step does not exceed the total step amount.");
}
_currentProgressStep.Text = $"{CurrentStep.Value} / {TotalStepCount.Value}";
float startPosition = _progressBar.GetX();
float totalWidth = startPosition + _progressBar.Width;
float stepSize = totalWidth / TotalStepCount.Value;
float currentPosition = stepSize * CurrentStep.Value;
_currentProgressStep.SetX(currentPosition - _currentProgressStep.Width);
}