左边是按钮,右边是ImageView。我希望按钮相对于 View 垂直居中,但不要和 View 一样高,比如当前高度的 1/4。
不幸的是,我知道如何在 RelativeLayout 中将某些东西垂直居中的唯一方法是使用 layout_alignTop/Bottom 到 View ,它会拉伸(stretch)按钮。我怎样才能避免这种情况? XML:
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/camera"
android:layout_alignBottom="@+id/camera"
android:gravity="center"
android:text="Tap to Take Picture"
android:textSize="10sp" />
<ImageView
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_toEndOf="@+id/cameraButton"
android:layout_toRightOf="@+id/cameraButton"
android:contentDescription="camera photo" />
</RelativeLayout>
(附带问题:有没有办法让 View 的高度与其宽度相同而无需硬编码 200dp?我想它可以通过编程方式完成,但更喜欢 XML)
最佳答案
给你!
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tap to Take Picture"
android:textSize="10sp" />
<ImageView
android:id="@+id/camera"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="camera photo" />
</LinearLayout>
关于android - 垂直居中按钮,同时限制高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33919414/