Android 如何将图像移动到线性布局的底部
我在尝试将图像放置在特定占位符的底部时遇到了一个问题。我尝试在线性布局和图像视图中包含重力底部,尝试设置边距和其他一些东西,但似乎无法使我的徽标显示在占位符的底部 下面是代码,下面是我试图做的一个例子。第一个图像是它当前的外观,第二个图像是我想要的:Android 如何将图像移动到线性布局的底部,android,Android,我在尝试将图像放置在特定占位符的底部时遇到了一个问题。我尝试在线性布局和图像视图中包含重力底部,尝试设置边距和其他一些东西,但似乎无法使我的徽标显示在占位符的底部 下面是代码,下面是我试图做的一个例子。第一个图像是它当前的外观,第二个图像是我想要的: <LinearLayout android:layout_width="100dp" android:layout_height="0dp" android:layout_weight="1" 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>
更新
感谢您提供的解决方案,但目前它不起作用。我将发布整个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>
更新: 因为您有嵌套布局。。使用此代码 您只需在
图像视图中将水平居中
更改为底部
代码
如图所示,徽标水平居中,因此您可以在ImageView
中添加此行,使其保持在中心位置
android:layout_gravity="center_horizontal"
使用相对布局以获得更好的方法
<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>
您可以使用RelativeLayout
移动相对布局中的任何内容,线性布局
将不会像相对布局那样有帮助,如果需要,请更进一步,然后学习约束布局
欲了解更多属性,请
使用以下代码:-
您需要做的是在要与屏幕底部对齐的视图之前添加以下视图。下面的代码应该会有所帮助
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
这将创建一个空视图,填充空空间并将下一个视图推到屏幕底部。我认为您可以在线性布局中使用相对布局。将相对布局作为线性布局的子级。在其宽度和高度上设置两个match_parent
。将图像放入相对布局中,并在图像道具上添加android:Layout\u alignParentBottom=“true”
。它应该可以解决您的问题为什么不使用RelativeLayout
?您的线性布局
的高度为0,重量为1,我可能错了,但它告诉我它的父级为线性布局
。请发布完整的XML布局好吗?@V-rundPuro-hit为什么不FrameLayout
,因为这是性能最好的一个Relative
更差:)Relative最适合移动对象。它也可以通过LinearLayout
来完成,但它没有提供灵活性,因为Relative
提供了。@IonutJ.Bejan如果更糟糕的话,他们没有首先开发它;)。但是框架布局更好,它不起作用,我不确定是否因为它在嵌套的线性布局中,我会发布整个xmlit实际上起作用了。这就是为什么我在示例中添加了image tooYeah,它在您的示例中起作用:)请参见更新xml,因为当我尝试在我的xml中执行相同操作时,它不起作用更新我的答案,请参见。这是可行的,但似乎不是最佳解决方案。因为relativelayout将覆盖整个屏幕。但是谢谢:)
<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>
android:layout_alignParentTop
android:layout_centerVertical
android:layout_below
android:layout_toRightOf
android:layout_centerHorizontal
android:layout_centerInParent
etc......
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>
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />