我试图将一个 ImageView 对象放置在线性布局对象(即两个按钮的父级)的左侧。我基本上想要两个按钮与屏幕水平居中,图像位置位于居中按钮的左侧。我仍然希望按钮保持水平居中,放置的 ImageView 不应该居中,只是放在一边。
任何建议都会很棒。我可能一直在用错误的布局来解决我的问题,但这是我尝试过的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/gameSettingsContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.vb1115.multchoicequestion.LaunchScreen"
android:orientation="vertical"
android:gravity="center_horizontal">
<LinearLayout
android:id="@+id/buttonContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ToggleButton android:id="@+id/mathButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Math"
android:textOn="Math"
android:text="Math"/>
<ToggleButton android:id="@+id/bioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Biology"
android:textOn="Biology"
android:text="Biology"/>
</LinearLayout>
<ImageView
android:id="@+id/animatedArrow"
android:src="@mipmap/green_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/buttonContainer"/>
</RelativeLayout>
最佳答案
虽然我没有 Android Studio 可以测试,但我已经可以做出一些假设...
第一,无需为根 RelativeLayout 使用 android:orientation="vertical"
或 android:gravity="center_horizontal"
...
第二,要定位 2 个子元素,请为 LinearLayout 使用 android:layout_centerHorizontal
,并为您的 ImageView 保留 android:layout_alignParentLeft
...
这应该会开始好一点...主要思想是在 RelativeLayout 中让 child 决定他们的位置通常更有效...希望这会有所帮助。
关于android - 将 ImageView 放置在居中按钮的左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279107/