android - 将 ImageView 放置在居中按钮的左侧

标签 android android-studio imageview android-togglebutton

我试图将一个 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_horizo​​ntal"...

第二,要定位 2 个子元素,请为 LinearLayout 使用 android:layout_centerHorizo​​ntal,并为您的 ImageView 保留 android:layout_alignParentLeft...

这应该会开始好一点...主要思想是在 RelativeLayout 中让 child 决定他们的位置通常更有效...希望这会有所帮助。

关于android - 将 ImageView 放置在居中按钮的左侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35279107/

相关文章:

android - 拉伸(stretch) ImageView 以填充水平 ScrollView 内的屏幕高度

android - 如何调试 AsyncTask 的 doInBackground 代码

android - 从位图中获取图像并将其放入字符串中

java - 使用 JSOUP 获取 HTML 字符串中的第一个有效图像

android - Gradle:不能排除两个log4j依赖关系之一?

java - 构建签名 apk 时出错,我该如何解决这个问题?

android - 无法获取未知属性 'manifestOutputDirectory'

Android如何使用复选框识别 ListView 中的项目

android - 如何获取 SharedPreferences 数据大小,可能吗?

Android:如何制作默认拨号器应用程序?