如何在不改变高度的情况下改变矩形的宽度
How to change width of rectangle shape without changing height
我正在尝试制作一个包含 5 列的动态条形图,该条形图会根据用户输入而变化。我定义了一个矩形 xml 可绘制形状,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp"
/>
<solid
android:color="#c73434"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke android:width="2px"
color="#ff00ffff" />
</shape>
我在我的 xml 布局文件中嵌入了 5 次条形,如下所示:
<ImageView
android:id="@+id/bar_1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="start"
android:src="@drawable/bar"/>
<ImageView
android:id="@+id/bar_2"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="start"
android:src="@drawable/bar"/>
etc...
我希望能够根据用户输入更改栏的宽度,但是当我调整 ImageView 的 "width" 属性 时,它也会按比例影响高度(所以整个条变小)。有什么建议么?有更好的方法吗?
我认为有两点:
- 使您的 ImageView 包装内容而不是 match_parent(对此不确定)
- 最重要的是,从您正在使用的背景可绘制对象中删除大小限制。我认为它保留了您的纵横比。
我正在尝试制作一个包含 5 列的动态条形图,该条形图会根据用户输入而变化。我定义了一个矩形 xml 可绘制形状,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp"
/>
<solid
android:color="#c73434"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke android:width="2px"
color="#ff00ffff" />
</shape>
我在我的 xml 布局文件中嵌入了 5 次条形,如下所示:
<ImageView
android:id="@+id/bar_1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="start"
android:src="@drawable/bar"/>
<ImageView
android:id="@+id/bar_2"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="start"
android:src="@drawable/bar"/>
etc...
我希望能够根据用户输入更改栏的宽度,但是当我调整 ImageView 的 "width" 属性 时,它也会按比例影响高度(所以整个条变小)。有什么建议么?有更好的方法吗?
我认为有两点:
- 使您的 ImageView 包装内容而不是 match_parent(对此不确定)
- 最重要的是,从您正在使用的背景可绘制对象中删除大小限制。我认为它保留了您的纵横比。