【问题标题】:Android : Rounded-edges view grow within another rounded-edges viewAndroid:圆边视图在另一个圆边视图中增长
【发布时间】:2016-10-19 14:37:52
【问题描述】:

我正在尝试在 Android 中制作两个 圆角视图,A 和 B。两个视图具有相同的圆角半径,但 B 在 A 内,或者 B 应该在 A 内。

B 的宽度根据下面的百分比是动态的。当百分比超过 5% 时,这非常有效。但是,由于百分比小于 5%,结果将如下面的故障图所示。它们看起来像完全独立的观点,尽管实际上是。我只需要在灰色区域内生长绿色部分。这如何实现?

理想情况下,它应该看起来像

但我没能做到。 :/我得到的数字

这是我所做的,定义如下所示的可绘制对象

<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>

【问题讨论】:

标签: android views rounded-corners


【解决方案1】:

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

<?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>

【讨论】:

  • 它不起作用。由于填充减小了 B 的大小,因此 B 的某些部分将被截断。
  • 我编辑了我的答案并删除了填充。您将通过中风达到目标。 @舒敏
  • 谢谢!我稍后会尝试。 :]
  • Solution 这对我来说效果更好。无论如何,谢谢@Smartiz! :]]]
猜你喜欢
  • 1970-01-01
  • 2021-04-20
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多