Android 如何将相同的圆角设置为线性布局?
我正在尝试使用线性布局制作单个条形图(不想使用MPAndroidChart或其他库)。Android 如何将相同的圆角设置为线性布局?,android,layout,android-drawable,cornerradius,Android,Layout,Android Drawable,Cornerradius,我正在尝试使用线性布局制作单个条形图(不想使用MPAndroidChart或其他库)。 这是我想要实现的目标: 如您所见,无论布局的宽度如何,拐角都具有相同的度/半径。 以下是我所做的: 这看起来不错,但问题来了:如果使用宽度较小的线性布局,那么半径就不能按我想要的方式工作 我希望两者的半径角相同,类似这样(蓝色部分应该被切断): 我希望我可以用一些perecent设置radius属性,但这是不可能的。布局如下: <androidx.constraintlay
这是我想要实现的目标:
如您所见,无论布局的宽度如何,拐角都具有相同的度/半径。
以下是我所做的:
这看起来不错,但问题来了:如果使用宽度较小的线性布局,那么半径就不能按我想要的方式工作
我希望两者的半径角相同,类似这样(蓝色部分应该被切断):
我希望我可以用一些perecent设置
radius
属性,但这是不可能的。布局如下:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint_layout_statistics_budgeting_recycler_view_chart"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="24dp"
android:background="@drawable/rounded_bar_chart_total"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image_view_statistics_budgeting_recycler_view_category_image">
<LinearLayout
android:id="@+id/linear_layout_statistics_budgeting_recycler_view_bar_chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_bar_chart_spent"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
```
怎么做有什么帮助吗?
您不必自己硬编码进度条。似乎材质组件库
实现了这一点(需要com.google.android.material:material:1.3.0'
)。检查github文档,下面是我做的一个示例:
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/linearProgressIndicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false" // static progress bar
android:progress="50"
app:trackCornerRadius="10dp"
app:trackThickness="20dp" />
它们显然不同,因为第一个xml有填充,我认为这是不必要的,第二个xml非常好,如果需要填充,为什么不在线性布局上添加填充呢?
<?xml version="1.0" encoding="utf-8"?>
<!--rounded_bar_chart_spent-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="48dp" />
<solid android:color="#5CE662" />
</shape>```
Any help how to do this?
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/linearProgressIndicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false" // static progress bar
android:progress="50"
app:trackCornerRadius="10dp"
app:trackThickness="20dp" />