首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将视图置于屏幕外的Android约束布局中

如何将视图置于屏幕外的Android约束布局中
EN

Stack Overflow用户
提问于 2022-02-23 18:03:25
回答 2查看 1.1K关注 0票数 1

我在一个约束布局中有一个View,我希望在一开始它应该在屏幕之外(然后慢慢地从右到左移动到屏幕)。现在,我需要一些像负面偏见或空白之类的东西。

我看过这个问题,How to achieve overlap/negative margin on Constraint Layout?。使用android:layout_marginTop="-25dp"接受的答案没有任何效果(而且视图的顶部受到约束,i为android:layout_marginTop="-25dp")。

我尝试了第二个最不正确的答案,并使用了下面的代码:

代码语言:javascript
复制
view.setTranslationX(view.getWidth() - 20);

这真的很管用。但是,问题是,当创建片段时,您首先会看到视图在很短的时间内不在左边。这不是我想要的。我希望在一开始就能看到布局的右边以外的视图,这样它以后就可以进入布局。

你知道我该怎么做吗?理想情况下,我希望以编程的方式这样做。

Update:以下是XML布局的代码,其中负边距没有任何影响:

代码语言:javascript
复制
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/game_test_background"
    tools:context=".MainActivity"
    android:id="@+id/constraintLayout">


    <ImageView
        android:id="@+id/imageView_RedRectange_Test"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginTop="-1250dp"

        app:layout_constraintWidth_percent="0.25"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.048"
        app:srcCompat="@drawable/red_rectangle" />


    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.102"
        app:layout_constraintHorizontal_bias="0.373"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.745"
        app:layout_constraintWidth_percent="0.12" />

</androidx.constraintlayout.widget.ConstraintLayout>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-23 18:24:32

好的,要有负值,可以使用translateX、translateY或TranslationZ。

在xml中如下所示:

代码语言:javascript
复制
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:translationX="-60dp"
    android:translationY="-90dp"
    android:translationZ="-420dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

或者以如下方式编程:

代码语言:javascript
复制
View view = ...;
view.setTranslationX(-60);
view.setTranslationY(-90);
view.setTranslationZ(-420);

然后,为了从右到左慢慢地引入它,您可以使用下面这样的动画()方法:

代码语言:javascript
复制
View view = ...;
view.animate().setDuration(1000).translationX(-600).start();
票数 3
EN

Stack Overflow用户

发布于 2022-02-25 00:20:36

app:layout_constraintWidth_percent具有负边距时,使用ImageView设置按钮的宽度存在问题。如果您可以为按钮设置一定的宽度(而不是0dp),那么问题就应该解决了。

如果将app:layout_constraintWidth_percent设置为一个使按钮的文本完全显示在一行上的值,问题也应该得到解决。

这里有一个简化的布局来演示这个问题。ConstraintLayout有两个视图,它们被简单地限制在父视图中,显示在布局的垂直中心。这两个视图之间没有依赖关系。此外,ImageView有一个-250dp的顶部边缘,因此它应该出现在布局的垂直中心上方。

代码语言:javascript
复制
<androidx.constraintlayout.widget.ConstraintLayout   
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="@android:color/holo_green_light">  
  
    <ImageView  
        android:id="@+id/redRectangle"  
        android:layout_width="100dp"  
        android:layout_height="30dp"  
        android:layout_marginTop="-250dp"  
        android:layout_marginEnd="16dp"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintTop_toTopOf="parent"  
        app:srcCompat="@drawable/red_rectangle" />  
  
     <Button  
            android:id="@+id/button"  
            android:layout_width="0dp"  
            android:layout_height="wrap_content"  
            android:text="Button"  
            android:textSize="18sp"  
            app:layout_constraintBottom_toBottomOf="parent"  
            app:layout_constraintEnd_toEndOf="parent"  
            app:layout_constraintStart_toStartOf="parent"  
            app:layout_constraintTop_toTopOf="parent"  
            app:layout_constraintWidth_default="percent"  
            app:layout_constraintWidth_percent="0.12" />  
  
</androidx.constraintlayout.widget.ConstraintLayout>

下面是发生的情况,然后ImageView的宽度从0dp更改为1dp的非零值。当宽度设置为0dp时,负边距似乎被忽略。只有当宽度设置为非零值时,ImageView才能正确放置。

更改按钮的宽度对ImageView的放置没有影响;但是,只有当按钮的宽度为非零时,按钮才会出现在适当的位置。

下面是当app:layout_constraintWidth_percent增加,使单词"Button“不截止时会发生的情况。

同样,ImageView的位置应该与按钮的宽度无关。相反,只有在设置app:layout_constraintWidth_percent时,按钮才会出现在正确的位置,这样单词" button“就没有截止。

这只是一个利润率为负的问题。正利润如预期的那样起作用。

这是一个奇怪的问题,所以您可能需要使用其他解决方案之一。

(ConstraintLayout版本2.1.3)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71241995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档