【发布时间】:2016-03-31 00:34:57
【问题描述】:
我想垂直放置两个圆圈,我希望它们的大小是父高度的一半。总之,请看下图。
我希望根据x(TextView 的高度)对圆圈进行缩放,并且可以根据系统偏好而有所不同。我在下面创建了一个布局,但它不起作用。这可以实现吗?
更新
根据答案,我为圆分配了一些任意宽度和高度,并将“fitXY”更改为“centerInside”(因为我发现 fitXY 不保持纵横比)。为了模拟不同的系统设置,我为 TextView 设置了任意高度。我将 ImageViews 的背景颜色设置为red 以查看它们的边界。
问题是,如果圆圈的宽度/高度小于 TextView 高度的 1/2(例如,圆圈宽度/高度=10dp,TextView 高度=100dp),则圆圈不会按比例放大。
如果圆圈的宽度/高度大于 TextView 高度的 1/2(例如,圆圈宽度/高度=200dp,TextView 高度=100dp),则圆圈会正确缩放,但不知何故有一些奇怪的空白边距圆圈的左/右侧。
更新结束
我创建了如下布局,但它不起作用。
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/circle"
android:scaleType="fitXY"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dp"/>
<ImageView
android:src="@drawable/circle"
android:scaleType="fitXY"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dp"/>
</LinearLayout>
<TextView
android:textAppearance="@android:style/TextAppearance.Large"
android:text= "If he's so smart, how come he's dead?"
android:layout_weight="1"
android:layout_width="0dp"
android:gravity="center_vertical"
android:layout_height="wrap_content"/>
</LinearLayout>
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#0000FF"/>
</shape>
【问题讨论】:
-
您尝试过的代码到底遇到了什么问题? (我的意思是,发生了什么事?)
-
似乎drawable的高度变成了我想要的高度的1/2,但是drawable的宽度似乎没有自动缩放,所以drawable没有显示为一个完美的圆形.
标签: android android-layout android-drawable