答案 0 :(得分:2)
像这样定义可绘制的资源文件
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="180"
android:endColor="@color/your_color"
android:startColor="@color/your_color2"
android:type="linear" />
</shape>
并将其用作视图的背景
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="@+id/view"
android:background="@drawable/your_gradient"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="30"
android:id="@+id/percentageTextview"
/>
</FrameLayout>
以及您的活动使用此
View background = (View) findViewById(R.id.view);
TextView textView = (TextView) findViewById(R.id.percentageTextview);
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int screenWidthInPix = displayMetrics.widthPixels;
ViewGroup.LayoutParams params = background.getLayoutParams();
params.width = (screenWidthInPix * Integer.parseInt(textView.getText().toString()))/100;
background.setLayoutParams(params);