如何将图像移至线性布局的底部

时间:2018-11-06 06:15:11

标签: android

我在尝试将图像放置在特定占位符底部时遇到问题。我已经尝试在线性布局和图像视图中包括重力的底部,尝试设置边距和其他一些东西,但是似乎无法让我的徽标出现在占位符的底部。

下面是代码,下面是我尝试执行的示例。第一张图片是当前的外观,第二张图片是我想要的:

<LinearLayout
    android:layout_width="100dp"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image" />

</LinearLayout>

enter image description here

更新

谢谢您的解决方案,但目前无法正常工作。我将发布整个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="match_parent"
    android:background="@drawable/back_client"
    android:orientation="vertical">

    <com.example.tecocraft.challenge_reward.widget.ButtonTextViewBold
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/_90sdp"
        android:text="@string/title"
        android:textAllCaps="false"
        android:textColor="@color/splsh_name"
        android:textSize="@dimen/_25sdp"
        android:textStyle="bold"
        android:paddingBottom="@dimen/_50sdp"
        android:textAlignment="center" />

        <LinearLayout
            android:id="@+id/new_challenge_btn"
            android:layout_width="@dimen/_175sdp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_10sdp"
            android:background="@drawable/btn_click_effect"
            android:layout_gravity="center"
            android:elevation="@dimen/_3sdp"
            android:padding="@dimen/_2sdp">

        <ImageView
            android:layout_width="@dimen/_35sdp"
            android:layout_height="@dimen/_35sdp"
            android:padding="@dimen/_5sdp"
            android:src="@drawable/ic_start_chal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="@dimen/_5sdp"
            android:text="@string/new_challenge"
            android:textColor="@color/white"
            android:textSize="@dimen/_14sdp" />
    </LinearLayout>

        <LinearLayout
            android:id="@+id/Resume_challenge_btn"
            android:layout_width="@dimen/_175sdp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_10sdp"
            android:background="@drawable/btn_click_effect"
            android:elevation="@dimen/_3sdp"
            android:layout_gravity="center"
            android:padding="@dimen/_2sdp">

            <ImageView
                android:layout_width="@dimen/_35sdp"
                android:layout_height="@dimen/_35sdp"
                android:padding="@dimen/_6sdp"
                android:src="@drawable/ic_recycle" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginRight="@dimen/_5sdp"
                android:padding="@dimen/_5sdp"
                android:text="@string/resume_challenge"
                android:textColor="@color/white"
                android:textSize="@dimen/_14sdp"
                android:layout_marginEnd="@dimen/_5sdp" />
        </LinearLayout>

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image"
            android:layout_gravity="center_horizontal"/>

    </LinearLayout>

    </LinearLayout>

5 个答案:

答案 0 :(得分:0)

已更新:

因为您有嵌套的布局。使用此代码

您只需要在centre_horizontally中将bottom更改为Imageview 代码

<LinearLayout
    android:layout_width="100dp"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cdmasim"
        android:layout_gravity="bottom"/>

</LinearLayout>

*************************************************** *************************

更新前:

Linear Layout中添加此行,以将布局项目与底部对齐

android:gravity="bottom"

如“图像徽标”中所示,水平居中,因此您可以在ImageView中添加此行以使其居中

    android:layout_gravity="center_horizontal"

Image

答案 1 :(得分:0)

您需要做的是在要与屏幕底部对齐的视图之前添加以下视图。以下代码应该会有所帮助。

<View
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" />

这将创建一个空白视图,填充空白空间并将下一个视图推到屏幕底部。

答案 2 :(得分:0)

我认为您可以在线性布局中使用相对布局。将“相对”布局设置为“线性”布局的子代。同时设置match_parent的宽度和高度。将您的图片放入相对布局,然后在图片道具上添加android:layout_alignParentBottom="true"。它应该解决您的问题

答案 3 :(得分:-1)

使用相对布局以获得更好的方法

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/login_fb"
    android:layout_centerInParent="true"/>

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/login_fb"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20dp"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

只学习Relative layout的属性或属性

这些是属性

android:layout_alignParentTop
android:layout_centerVertical
android:layout_below
android:layout_toRightOf
android:layout_centerHorizontal
android:layout_centerInParent
etc......

您可以使用RelativeLayout在相对布局内移动任何内容,Linear layout不会像Relative那样帮助您,并且如果想要更进一步,请学习Constraint Layout

有关更多属性,请visit

enter image description here

答案 4 :(得分:-1)

    use this code:-

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ImageView
         android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:src="@drawable/ic_launcher_background" />
    </RelativeLayout>
</LinearLayout>