【问题标题】:Child views inside MaterialCardView shrinking and vanishingMaterialCardView 内的子视图缩小和消失
【发布时间】:2019-01-03 15:36:15
【问题描述】:

android.support.design.card.MaterialCardView 已用于原生 axml 布局并具有如下子视图,我的目标是在发生某些事件时不断切换边框,即中风,例如:按钮单击并还原它,但 @987654326 中的每一个变化@子视图逐渐缩小,最终宽度和高度变为0,使内部视图完全消失。

MaterialCardView 布局代码:


<?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>

PageRenderer 代码


  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;
            }
        }

点击几下后:

我尝试使用其他布局RelativeLayout,这很有效,还尝试增加 LinearLayout 的高度和宽度,并将填充设置为 0 到 min,如上所示,这也不起作用,在调试时我注意到触发了填充MaterialCardView 这可能导致它缩小。 还尝试将cardview的填充设置为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

【问题讨论】:

  • 你想达到什么目的
  • @G.hakim 我的目标是在某些事件发生时不断切换边框,即中风,例如:按钮单击并还原它。

标签: android xamarin.forms xamarin.android android-cardview


【解决方案1】:

这个类似乎存在一个问题,导致其内容填充在更改 StrokeWidth 值后不断增加。

每次使用这行代码将 StrokeWith 设置为 0 时,尝试重置 ContentPadding

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

希望这会有所帮助。-

【讨论】:

  • 在这上面浪费了这么多时间,我恢复使用正常的CardView 并切换strokebackground 我不知道有SetContentPadding 用于CardView 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 2018-10-30
相关资源
最近更新 更多