代码之家  ›  专栏  ›  技术社区  ›  Ashvin solanki

视图上不必要的填充或边距[Android/Kotlin]

  •  0
  • Ashvin solanki  · 技术社区  · 7 年前

    我使用布局检查器捕获布局

    我的视图的XML文件

    <?xml version="1.0" encoding="utf-8"?>
    <package.MyView
    xmlns:android="http://schemas.android.com/apk/res/android"
    
    xmlns:app="http://schemas.android.com/apk/res-auto"
    
    android:layout_width="match_parent"
    
    android:layout_height="wrap_content"
    android:padding="10dp">
    
    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
    
        xmlns:app="http://schemas.android.com/apk/res-auto"
    
        android:layout_width="match_parent"
    
        android:layout_height="wrap_content"
    
        app:cardCornerRadius="6dp"
    
        android:background="#FFF"
        android:foregroundGravity="center"
        app:cardElevation="6dp">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text"
            android:gravity="center"
            android:inputType="textMultiLine"
            android:ems="10" android:padding="4dp"/>
    </android.support.v7.widget.CardView>
    </package.MyView>
    

    class MyView :RelativeLayout
    {
        constructor(context: Context?) : super(context)
        constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
        constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    
        override fun onAttachedToWindow() {
            super.onAttachedToWindow()
    
            /*var Anim:Animation = AnimationUtils.loadAnimation(this.context,R.anim.card_flip_left_in)
            this.animation=animation
            animation.start()*/
           // this.setPadding(10,10,10,10)
                    val animation = AnimationUtils.loadAnimation(this.context, R.anim.slide_right_in)
                    animation.startOffset = 0
                    this@MyView.startAnimation(animation)
        }
    
        override fun onDraw(canvas: Canvas?) {
            super.onDraw(canvas)
        }
    
        override fun onDetachedFromWindow() {
            super.onDetachedFromWindow()
    
        }
    }
    

    幻灯片右对齐.xml 动画文件

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="2000"/>
    </set>
    

    问题:我不明白为什么在背景绘制时在Cardview背景的顶部和底部添加边距或填充。。。。

    测试完成:模拟器和真实设备Vivo V7 Plus

    enter image description here

    问题是

    enter image description here

    我的视图大小如预期。。。。

    enter image description here

    CardView的尺寸与预期一致,但这里的背景图位置不对,如上图所示

    0 回复  |  直到 7 年前