Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将图像视图放置在中间按钮上方_Android_Android Layout - Fatal编程技术网

Android 将图像视图放置在中间按钮上方

Android 将图像视图放置在中间按钮上方,android,android-layout,Android,Android Layout,我正在尝试获得以下输出: 到目前为止,我尝试了以下代码: 对于这个愚蠢的问题,我是android新手。如果可能的话,请用合适的解决方案向我详细解释这个问题。用以下代码替换您的代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.andr

我正在尝试获得以下输出:

到目前为止,我尝试了以下代码:


对于这个愚蠢的问题,我是android新手。如果可能的话,请用合适的解决方案向我详细解释这个问题。

用以下代码替换您的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/back"
    tools:context="com.cash.nikito.Contact">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <!-- add src attribute here -->
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:orientation="horizontal"
        android:padding="20dp">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button 1" />

        <Button
            android:id="@+id/buttonUpload"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Upload" />
    </LinearLayout>

</RelativeLayout>

您有许多解决方案,其中之一是:

将方向设置为垂直

在添加另一个方向为水平的线性布局后,插入两个按钮。

试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/toolbar">


<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >


<ImageView
    android:layout_width="wrap_content"
    android:id="@+id/ivBack"
    android:src="@drawable/ic_launcher_background"
    android:layout_height="wrap_content" />



</android.support.v7.widget.Toolbar>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:layout_height="wrap_content">


        <Button
            android:layout_width="0dp"
            android:id="@+id/bChoose"
            android:layout_weight="1"
            android:layout_height="wrap_content" />


        <Button
            android:layout_width="0dp"
            android:id="@+id/bUpload"
            android:layout_weight="1"
            android:layout_height="wrap_content" />


    </LinearLayout>


</RelativeLayout>


</LinearLayout>

与所有其他答案不同,我想向你解释你应该使用的最佳布局。为了更好的响应布局,考虑使用约束布局-非常容易使用和舒适。此外,当您在android:layout\u weight上使用LinearLayout时,它会使您的布局变慢-显示布局引擎需要比constraintLayout更多的时间来显示布局。以下是使用constraint layout的示例:


哇,这看起来棒极了!
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:padding="20dp"> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@+id/toolbar">


<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >


<ImageView
    android:layout_width="wrap_content"
    android:id="@+id/ivBack"
    android:src="@drawable/ic_launcher_background"
    android:layout_height="wrap_content" />



</android.support.v7.widget.Toolbar>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:src="@mipmap/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:layout_height="wrap_content">


        <Button
            android:layout_width="0dp"
            android:id="@+id/bChoose"
            android:layout_weight="1"
            android:layout_height="wrap_content" />


        <Button
            android:layout_width="0dp"
            android:id="@+id/bUpload"
            android:layout_weight="1"
            android:layout_height="wrap_content" />


    </LinearLayout>


</RelativeLayout>


</LinearLayout>
    <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="324dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:src="@drawable/common_google_signin_btn_icon_dark"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/guideline2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline2"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />


</androidx.constraintlayout.widget.ConstraintLayout>