【问题标题】:Slide Up Layout and Stretch the height向上滑动布局并拉伸高度
【发布时间】:2018-06-21 13:32:21
【问题描述】:

我有这个布局:

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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/blue_pattern_repeated"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/layout_splash_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="8dp"
    >

    <RelativeLayout
        android:id="@+id/layout_splash_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="170dp"
        >

        <ImageView
            android:id="@+id/ivLogo"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            app:srcCompat="@mipmap/logo_exa"
            android:layout_alignParentTop="true"
            />

        <LinearLayout
            android:id="@+id/layout_loader_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="8dp"
            android:layout_below="@+id/ivLogo"

            >
        </LinearLayout>

    </RelativeLayout>


</LinearLayout>

如何将@+id/layout_splash_content 发送到父级的顶部?

我有这个幻灯片动画,但在某些设备上无法使用顶部位置,有时未与父级顶部对齐...

在一些主要活动中:

    private void SlideUpLogo() {
    Slide(layoutSplashContent, -0.65f, null);
}

在基础活动中:

protected void Slide(View view, float toYValue, Animation.AnimationListener listener) {
    ViewGroup.LayoutParams viewLP = view.getLayoutParams();
    ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(viewLP);

    TranslateAnimation ta = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, lp.topMargin,
            Animation.RELATIVE_TO_SELF, toYValue);

    ta.setDuration(500);
    ta.setFillAfter(true);
    ta.setAnimationListener(listener);
    view.startAnimation(ta);
}

【问题讨论】:

    标签: android android-animation android-linearlayout android-relativelayout


    【解决方案1】:

    这不是 ConstraintLayout 的工作方式。 MATCH_PARENT 在约束布局的情况下没有任何价值。在上述情况下,您的布局“@+id/layout_splash_container”应如下所示,

    <LinearLayout
    android:id="@+id/layout_splash_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:padding="8dp">
    

    它将占据父级的所有可用宽度和高度。您也可以尝试对简单结构使用相对或框架布局。

    【讨论】:

    • 感谢 Pratik,它正在工作,但我必须删除幻灯片效果,有办法实现吗?..
    • 您在动画方面遇到了什么问题?您是否尝试将约束布局的宽度更改为 wrap_content?
    • 你的回答很好,现在我的布局延伸到高度......但我想要标志的软滑,但在某些设备中,顶部的值不一样......请检查 slideUpLogo 和 slide 方法...
    • 您在 dp 中设置了边距,而在代码中将顶部边距作为浮点值获取。如果 m 没有错,系统会在您使用 dp 值喂它时期望它为 px 值。
    • 谢谢@Pratik,很抱歉迟到了,最后我删除了动画,因为无法更新向上滑动的布局的顶部和高度...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2014-01-17
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多