android - RingShapeDrawable 的半径设置为父级的高度而不是宽度

标签 android xml layout drawable

我目前正在使用可绘制对象来获得环形 ImageView 。

问题是:我在一行中有两个这样的 ImageView ,它们的权重都为 1,因此它们都具有相同的宽度。形状环可绘制对象的 xml 标记属性“innerRadiusRatio”依赖于可绘制对象的宽度。因此,如果 imageview 的宽度变得大于高度,环形将被裁剪。有什么方法可以使 innerRadiusRatio 依赖于高度而不是宽度?或者甚至将其 scaleType 设置为“fitCenter”或类似的东西?

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_height="64dip"
          android:layout_width="match_parent">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/ring_shape_drawable"
    android:src="@drawable/test_button_drawable"/>

<ImageView        
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/ring_shape_drawable"
    android:src="@drawable/test_button_drawable"/>
</LinearLayout>

这是可绘制形状的 xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="2.8"
    android:thicknessRatio="30"
    android:useLevel="false"
    android:shape="ring" >

    <solid android:color="@color/custom_red" />

</shape>

这是结果(我不想要裁剪的 ringShapeDrawable): enter image description here

这将是期望的结果: enter image description here

感谢您的帮助:)

最佳答案

由于您在 LinearLayout 中指定了一个恒定高度,您应该能够使用 innerRadius属性(property)。您可能也希望将 thicknessRatio 切换为具有设定大小的厚度。

我查看了 GradientDrawable 的源代码,似乎没有一种简单的方法让它使用高度,所以如果 innerRadius 不合适,你可能不得不将每个 ImageView 包装在另一个 LinearLayout 并以 android:gravity 居中。这样做需要可绘制对象确定 ImageView 的大小或手动指定它,因此 innerRadius 可能更好。

关于android - RingShapeDrawable 的半径设置为父级的高度而不是宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14649461/

相关文章:

java - 约束布局在不同设备上执行不同

android - Android ROOM保存Map <String,Object>

android - 什么是 setContentView(R.layout.main)?

android - 在不止一个对象上添加 onclick

java - 如何展平2个不同的图像层?

android - 如何知道一个应用程序将在android中被卸载?

XML Schema 为多个子元素添加唯一的 id

java - WebResource返回xml

c# - 如何在现有 XML 中添加子元素

html - 将 P 标签置于 H1 下方的问题