[英]Android Views pushed off of LinearLayout
I have a vertical LinearLayout
with a TextView
, a custom view (replaced with VideoView
to illustrate the problem), and a Button
.我有一个带有
TextView
的垂直LinearLayout
,一个自定义视图(替换为VideoView
来说明问题)和一个Button
。 I want the TextView
horizontally centered on the top, and the Button
horizontally centered on the bottom (as well as vertically centered in the remaining space between VideoView
and bottom of the screen).我希望
TextView
水平居中在顶部, Button
水平居中在底部(以及垂直居中在VideoView
和屏幕底部之间的剩余空间中)。 The VideoView
should occupy at most the space between the TextView
and the Button
. VideoView
最多应该占据TextView
和Button
之间的空间。 However, in my implementation the VideoView
pushes the TextView
and the Button
off of the screen, despite the height being set to wrap_content
.然而,在我的实现中,
VideoView
将TextView
和Button
推离屏幕,尽管高度设置为wrap_content
。 Everything works if I set the VideoView
height to a manual height.如果我将
VideoView
高度设置为手动高度,一切正常。 What am I doing wrong?我究竟做错了什么? I tried playing around with layout weights but couldn't get anything working.
我尝试使用布局权重,但无法正常工作。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/colorBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Space
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="button" />
<Space
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Jeff,杰夫,
I believe it's easier to use constraint layout instead here.我相信在这里使用约束布局更容易。 The current problem is that you can't determine the size of videoview because it has a wrap_content height
当前的问题是您无法确定 videoview 的大小,因为它具有 wrap_content 高度
which is trying to take over the whole space in this case在这种情况下,它试图接管整个空间
In Constraint layout if you are trying to achieve the following:如果您尝试实现以下目标,请在约束布局中:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="?android:attr/colorBackground"
android:clickable="true"
android:focusable="true">
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="title"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/titleTextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
if you are interested on how it should be fixed in LinearLayout I believe you will need to remove all <Space and add the weight to the VideoView如果您对如何在 LinearLayout 中修复它感兴趣,我相信您需要删除所有 <Space 并将权重添加到 VideoView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.