【问题标题】:Not shown TextView after setting VISIBLE from GONE in animation在动画中从 GONE 设置 VISIBLE 后未显示 TextView
【发布时间】:2016-11-24 12:10:43
【问题描述】:

尝试使用“滚动”动画将 LinarLayout 从 GONE 状态显示为 VISIBLE。在此布局上存在 TextView。似乎一切正常,但动画后未显示 TextView。 我做错了什么?

       <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:id="@+id/ll_info">
            <TextView
                android:id="@+id/bayer_note"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:text="@string/buyer_note"/>
            <RelativeLayout
                android:id="@+id/button_continue"
                android:layout_width="match_parent"
                android:layout_height="54dp"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:layout_marginRight="@dimen/activity_horizontal_margin"
                android:background="@drawable/button_register"
                android:drawableEnd="@drawable/ic_arrow_forward_white_24dp">

                <TextView
                    android:id="@+id/btn_cont_caption"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="Войти"
                    android:textColor="@android:color/white"
                    android:textSize="20sp"
                    android:fontFamily="fonts/SF-UI-Display-Regular.ttf"
                    android:layout_centerInParent="true" />

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:id="@+id/imageView"
                    android:layout_centerVertical="true"
                    android:layout_alignParentEnd="false"
                    android:src="@drawable/ic_door"
                    android:layout_toRightOf="@+id/btn_cont_caption"
                    android:layout_marginLeft="10dp"
                    android:scaleType="fitCenter" />
            </RelativeLayout>
        </LinearLayout>

及动画代码:

final LinearLayout ll_info = (LinearLayout)findViewById(R.id.ll_info);

        class scaleAnimation extends Animation {
                    public LinearLayout ll;
                    public int newHeight;

                    public void scaleTopHeight(int height)
                    {
                        newHeight = height;
                    }

                    public void setLayout(LinearLayout layout) {
                        ll = layout;
                    }
                }

                final LinearLayout ll_info = (LinearLayout)findViewById(R.id.ll_info);

                scaleAnimation h = new scaleAnimation() {
                    @Override
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        ll.getLayoutParams().height = interpolatedTime == 1
                                ? LinearLayout.LayoutParams.WRAP_CONTENT
                                : (int)(newHeight * interpolatedTime);
                        ll.requestLayout();
                    }
                };
                h.setDuration(300);
                h.setLayout(ll_info);
        ll_info.setVisibility(View.VISIBLE);
                ll_info.measure(View.MeasureSpec.makeMeasureSpec(((LinearLayout)ll_info.getParent()).getWidth(), View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(((LinearLayout)ll_info.getParent()).getHeight(), View.MeasureSpec.AT_MOST));
                h.scaleTopHeight(ll_info.getMeasuredHeight());
        ll_info.getLayoutParams().height=1;
                ll_info.startAnimation(h);

【问题讨论】:

  • 如果可能有帮助,请尝试更改 textview 的 textColor。
  • @Drv,动画后 TextView 不存在并且此 TextView 层上的 LinearLayout 具有较小的高度。我认为原因不是文字颜色...
  • 您需要编写适当的代码来设置线性布局的高度。
  • @Drv,我做错了什么?
  • 抱歉,我真的不明白你想做什么,所以我无法提供帮助。

标签: android animation layout visible


【解决方案1】:
<RelativeLayout
            android:id="@+id/button_continue"
            android:layout_width="match_parent"
            android:layout_height="54dp"

            **android:layout_centerHorizontal="true"** /////  

删除此

            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:background="@drawable/button_register"
            android:drawableEnd="@drawable/ic_arrow_forward_white_24dp">

            <TextView
                android:id="@+id/btn_cont_caption"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="Войти"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                android:fontFamily="fonts/SF-UI-Display-Regular.ttf"
                android:layout_centerInParent="true" />

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:id="@+id/imageView"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="false"
                android:src="@drawable/ic_door"
                android:layout_toRightOf="@+id/btn_cont_caption"
                android:layout_marginLeft="10dp"
                android:scaleType="fitCenter" />
        </RelativeLayout>

我认为这是您的 xml 文件问题,您的相对布局在 center_horizo​​ntal 中,所有子项都在 parent.so 的中心。所以 textview 隐藏在 imageview 上。试试看,让我知道。

【讨论】:

  • 抱歉,但不是此文本视图未显示。 id.buyer_note 不可见。但如果我没有设置 Visibly GONE,那么在 Activity 启动后,所有视图都具有正确的大小和可见性。但我只需要通过按钮单击来显示 ll_info。如果我将设计时间可见性更改为 GONE 并在运行时 ll_info 布局显示上显示可见性但没有 TextView id.buyer_info。
猜你喜欢
  • 1970-01-01
  • 2014-08-18
  • 2011-05-01
  • 2011-11-20
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多