Android:圆角边视图在另一个圆边视图中增长

时间:2016-10-19 01:29:58

标签: android views rounded-corners

我想在Android中制作两个圆边视图,A和B.两个视图具有相同的角半径,但B在A或B内应该看起来像在A中。

根据下面的百分比,B&#39的宽度是动态的。当百分比超过5%时,这完全正常。但是,由于百分比低于5%,它将变成下面的失败数字。它们看起来像完全独立的观点,尽管它们实际上是。我需要绿色部分只在灰色区域内生长。如何实现这一目标?

理想情况下,它看起来应该是这样的

但我没能成功。 :/我得到的数字 A is the gray view and B is green

这就是我所做的,定义一个drawable,如下所示

<RelativeLayout
    android:id="@+id/percentageBarViewGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  <View
    android:id="@+id/a_Bar"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:background="@drawable/progress_bar_empty_background"/>

  <View
    android:id="@+id/b_Bar"
    android:layout_width="0dp"
    android:layout_height="20dp"
    android:background="@drawable/progress_bar_progressing_background"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您应该使用嵌套视图和填充。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

    <FrameLayout
    android:id="@+id/a_Bar"
    android:layout_width="match_parent"
    android:layout_height="20dp"
    android:background="@drawable/progress_bar_empty_background">
    <View
        android:id="@+id/b_Bar"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/progress_bar_progressing_background"/>
    </FrameLayout>
</RelativeLayout>

最后使用与progress_bar_progressing_background相同的颜色为progress_bar_empty_background添加笔画。

类似的东西:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
   <corners android:radius="3dp"/>
   <stroke android:width="2dp" android:color="#222"/>
   <solid android:color="#fff"/>
</shape>
相关问题