代码之家  ›  专栏  ›  技术社区  ›  Morse unmounted

材料卡中的子视图视图缩小和消失

  •  2
  • Morse unmounted  · 技术社区  · 6 年前

    android.support.design.card.MaterialCardView 已在本机中使用 axml 布局并具有子视图,如下所示,我的目标是不断切换边框,即在发生某些事件时进行笔画,例如:按钮单击并还原它,但 strokeWidth 子视图逐渐缩小,最后宽度和高度变为0,使内部视图完全消失。

    材料卡视图布局代码:


    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.card.MaterialCardView  
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_margin="10dp"
                  app:cardCornerRadius="10dp"
                  app:strokeColor="#123456"
                  app:strokeWidth="3dp"
                  android:id="@+id/material_card"
                  android:padding="5dp"
                  android:layout_gravity="center_horizontal">
                  <LinearLayout android:layout_width="fill_parent"
                                  android:layout_height="240dp"
                                  android:orientation="vertical"
                                android:padding="8dp"
                                android:id="@+id/linearLayoutCard">
                    <Button android:id="@+id/button"
                            android:text="Click me"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"/>
                    <TextView android:id="@+id/textview"
                              android:text ="Border in"
                              android:layout_below="@id/button"
                              android:layout_width="fill_parent"
                              android:layout_height="wrap_content"/>
                  </LinearLayout>
    </android.support.design.card.MaterialCardView>
    

    页面呈现程序代码


      public void OnClick(Android.Views.View v)
            {
                // throw new NotImplementedException();
                if (_yes)
                {
                    material_card.StrokeColor = Android.Graphics.Color.ParseColor("#FFFFFF");
                    material_card.StrokeWidth = 0;
                    //material_card.LayoutParameters.LayoutAnimationParameters
                    linearLayoutCard?.SetMinimumHeight(material_card.Height+20);
                    linearLayoutCard?.SetMinimumWidth(material_card.Width+20);
                    linearLayoutCard.SetPadding(0, 0, 0, 0);
                    textview.Text = "Border out";
                    _yes = false;
                   // return true;
                }
                else
                {
                    material_card.StrokeColor = Android.Graphics.Color.ParseColor("#123456");
                    material_card.StrokeWidth = 20;
                    linearLayoutCard?.SetMinimumHeight(material_card.Height);
                    linearLayoutCard?.SetMinimumWidth(material_card.Width);
                    linearLayoutCard.SetPadding(0, 0, 0, 0);
                    textview.Text = "Border in";
                    _yes = true;
                    //return true;
                }
            }
    

    点击几下后:

    Issue_image

    我尝试使用其他布局 RelativeLayout 它起作用,还尝试增加线性布局的高度和宽度,并将填充设置为0到min,如上所述,这也不起作用,当调试时,我注意到填充会触发 MaterialCardView 这可能导致它收缩。 还尝试将cardview的padding设置为0,但仍然没有运气。

    material_card.SetPadding(0, 0, 0, 0);
    linearLayoutCard.LayoutParameters.Width = material_card.Width;
    linearLayoutCard.LayoutParameters.Height =material_card.Height;
    

    为什么内部视图自映射到 match_parent / fill_parent ?

    Github的完整代码: github.com/pmahend1/TestMaterialCardView

    1 回复  |  直到 6 年前
        1
  •  1
  •   pinedax    6 年前

    该类似乎存在问题,导致其内容填充在更改 StrokeWidth 价值观。

    尝试重置 内容填充 每次你设置 StrokeWith 使用此行代码:

    material_card.SetContentPadding(0, 0, 0, 0);

    希望这有帮助。-