简体   繁体   English

Android 视图被线性布局推开

[英]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最多应该占据TextViewButton之间的空间。 However, in my implementation the VideoView pushes the TextView and the Button off of the screen, despite the height being set to wrap_content .然而,在我的实现中, VideoViewTextViewButton推离屏幕,尽管高度设置为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM