Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将前景属性设置为其他非FrameLayout视图_Android_View_Drawable_Android View_Android Framelayout - Fatal编程技术网

Android 如何将前景属性设置为其他非FrameLayout视图

Android 如何将前景属性设置为其他非FrameLayout视图,android,view,drawable,android-view,android-framelayout,Android,View,Drawable,Android View,Android Framelayout,我想知道如何在不同于FrameLayout的视图中应用或模拟前景效果,如LinearLayout或RelativeLayout 这就是我现在拥有的: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-a

我想知道如何在不同于FrameLayout的视图中应用或模拟前景效果,如LinearLayout或RelativeLayout

这就是我现在拥有的:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/row_background"
    android:foreground="@drawable/foreground_row">

    ...

</FrameLayout>

...
我想要像这样的东西:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/row_background"
        app:foreground="@drawable/foreground_row">

        ...

    </RelativeLayout>

...

提前谢谢

想法是用框架布局包围布局,并将选择器和onClick事件设置为此布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/selectableItem"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:foreground="@drawable/foreground_row"
    >

   <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/row_background">

        ...

    </RelativeLayout>
</FrameLayout>

...
你可以在我的博客上找到完整的解释:


或者您可以扩展rhis
FRelativeLayout

这里有一个可能的实现,它还支持检查布局

几乎相同的解决方案可应用于您希望的任何布局视图(仅CTOR不同)

attr.xml

<declare-styleable name="LinearLayoutEx" tools:ignore="MissingDefaultResource">
    <attr name="foreground"/>
</declare-styleable>

使用Gradle集成签出库。它支持以下视图

  • 前景图像视图
  • 前地面按钮
  • 前景文本视图
  • 前景图像按钮
  • 前景编辑文本
  • 前景色
  • 前端线性布局
  • 前景相对论
  • 前场网格布局
  • 前景网格视图
  • 前景水平滚动视图
  • 前景视图
  • 前景滚动视图前景图像视图

如果您想使用
前台
属性仅在
图像视图
上添加涟漪,那么除了将其包装在
中或将其子类化之外,还有很多方法。
<declare-styleable name="CheckableLinearLayout">
    <attr name="foreground"/>
</declare-styleable>
class LinearLayoutEx @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0) : LinearLayout(context, attrs, defStyle) {
    var foregroundDrawable: Drawable? = null
        set(value) {
            field = value
            invalidate()
        }

    init {
        val a = context
                .obtainStyledAttributes(attrs, R.styleable.LinearLayoutEx, defStyle, 0)
        foregroundDrawable = a.getDrawable(R.styleable.LinearLayoutEx_foreground)
        a.recycle()
    }

    override fun drawableStateChanged() {
        super.drawableStateChanged()
        val drawable = background
        var needRedraw = false
        val myDrawableState = drawableState
        if (drawable != null) {
            drawable.state = myDrawableState
            needRedraw = true
        }
        if (foregroundDrawable != null) {
            foregroundDrawable!!.state = myDrawableState
            needRedraw = true
        }
        if (needRedraw)
            invalidate()
    }

    override fun onSizeChanged(width: Int, height: Int, oldwidth: Int, oldheight: Int) {
        super.onSizeChanged(width, height, oldwidth, oldheight)
        foregroundDrawable?.setBounds(0, 0, width, height)
    }

    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)
        foregroundDrawable?.draw(canvas)
    }

    @SuppressLint("ClickableViewAccessibility")
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onTouchEvent(e: MotionEvent): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && e.actionMasked == MotionEvent.ACTION_DOWN)
            foregroundDrawable?.setHotspot(e.x, e.y)
        return super.onTouchEvent(e)
    }

}
<declare-styleable name="LinearLayoutEx" tools:ignore="MissingDefaultResource">
    <attr name="foreground"/>
</declare-styleable>