Tag: android custom view

在自定义视图构造函数中获取android:ellipsize属性的值

如何获得自定义View (扩展TextView )中的属性android:ellipsize的值? 我尝试了这些方法,但没有奏效: Log.d(TAG, attrs.getAttributeIntValue(ANDROID_NAMESPACE, “ellipsize”, -1)); Log.d(TAG, attrs.getAttributeValue(ANDROID_NAMESPACE, “ellipsize”)); Log.d(TAG, attrs.getAttributeUnsignedIntValue(ANDROID_NAMESPACE, “ellipsize”, 111)); Log.d(TAG, attrs.getAttributeListValue(ANDROID_NAMESPACE, “ellipsize”, new String[] {“x”, “marquee”}, -1)); 注意: attrs是AttributeSet (构造函数参数), ANDROID_NAMESPACE是”http://schemas.android.com/apk/res/android” 。

在自定义视图中绘制自定义视图

我正在处理自定义视图TreeView ,它将显示给定根节点的树。 我正在使用Kotlin语言。 以下是目前的部分内容: override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) drawNodeAndChildren(canvas, rootNode) } /** * Draws a representation of a node and its children onto the canvas. * … */ private fun drawNodeAndChildren(canvas: Canvas, node: TreeNode, …): Int { … // Calculate coordinates of rect val top = … val bottom = … val left […]

Android:自定义按钮上的背景颜色没有正确设置前的棒棒糖

当我使用一个标准的按钮 样式: match_parent @dimen/login_btn_height false @color/white @dimen/login_buttons_bottom_margin 主题: @color/CrGreen @color/CrGreen @color/CrGreen 一切工作正常,背景颜色设置在每个api级别16+ 但是当我使用我的自定义按钮(用kotlin编写,需要自定义字体) open class CheckrobinButton : Button { val fontPath = “fonts/CoreSansG55.otf” constructor(context: Context) : super(context) { init() } constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) { init() } constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) { init() } constructor(context: Context?, […]

在Android中使用kotlin进行自定义视图的类DSL的OnClick监听器

我正在尝试创建一个类似DSL的OnClick监听器(在kotlin中),用于在我正在使用的Android项目中使用多个位置的自定义视图。 该视图具有ImageView,主要textview和辅助textview。 我正在试图创建一个监听器帮手,它允许你只覆盖一个接口的特定方法,而不是所有的( 本文的启发)。 但是我无法得到它的工作。 实际上它甚至不能使用普通的OnClick监听器。 这是我的ErrorMessageView: class ErrorMessageView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyle: Int = 0, defStyleRes: Int = 0 ) : FrameLayout(context, attrs, defStyle, defStyleRes) { private var mOnClickListener: OnErrorMessageViewClickListener? = null init { LayoutInflater.from(context).inflate( R.layout.custom_errorview, this, true) ButterKnife.bind(this) } interface OnErrorMessageViewClickListener { fun onImageClick() fun onPrimaryTextClick() fun onSecondaryTextClick() […]

在kotlin中定制ProgressBar

我试图做一个自定义ProgressBar的方式,我做了很多以前的自定义布局。 这一次我有问题。 我可以用这个xml实现所需的外观: 然而,最终的目标是在自定义类中进行一些自定义,所以xml缩小到这个范围: 这是自定义类: class HorizontalProgressBar : ProgressBar { constructor(context: Context) : super(context) { initialSetup() } constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialSetup() } constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { initialSetup() } fun initialSetup() { max = 100 isIndeterminate = false progressDrawable = context.getDrawable(R.drawable.progress_bar) } } 第二个构造函数被解雇了,但是这个酒吧没有风格。 […]

Android – ViewDataBinding.inflate不会膨胀嵌套的自定义视图

我真的难住Android的视图通胀问题。 我看了其他示例代码使用android.databinding,我似乎无法理解为什么这不起作用。 任何有Android的经验的人可以提供一些建议? 我创建了一个简单的自定义视图: class TestView(context: Context) : View(context) { private var testCallBack: TestCallback? = null fun setTestCallback(callback: TestCallback) { testCallBack = callback } interface TestCallback { fun test() } } 然后把它包含在一个简单的布局中: <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".application.home.HomeActivity"> <com.test.TestView android:id="@+id/testView" android:layout_width="match_parent" android:layout_height="match_parent"/> </android.support.constraint.ConstraintLayout> </layout> 然后我膨胀我的片段onCreateView方法的布局,并使用视图绑定库,尝试设置我的自定义视图的回调: class TestFragment : TiFragmentBase<TiPresenterHolder, TiView>(), […]

拦截触摸事件并根据运动事件状态重新引导它

我在Android中创建了一个从RelativeLayout继承的自定义视图。 在这个视图内部是一个OverScroller,当触摸事件发生时用于处理视图的滚动: class MyCustomView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : RelativeLayout(context, attrs, defStyleAttr) { constructor(context: Context) : this(context, null, 0) constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0) private val scroller = OverScroller(context, FastOutLinearInInterpolator()) private var currentY = 0 private val gestureDetector = GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() { override fun onDown(event: MotionEvent?): Boolean { // […]

在TextView的文本和边界之间获取空间

我正在尝试获取文本和TextView左侧之间的距离。 它使用属性android:gravity="center" 。 我想要得到红色条的距离(这个红色的条不是布局的一部分),以中心的蓝色按钮。 我该怎么办? 黑暗区域代表了TextView的界限。 我不想使用CompoundDrawable,因为这个视图会随机更改按钮的颜色。 视图代码(用Kotlin编写): class BallTextView: TextView { private lateinit var ballPaint : Paint private var ballRadius : Float = 10f private var ballColor : Int = Color.BLACK constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs) { initializeAttributes(attrs) configBall() } override fun onDraw(canvas: Canvas) { canvas.drawCircle(ballRadius, height.toFloat()/2, ballRadius, ballPaint) super.onDraw(canvas) } fun […]

在布局中使用自定义Kotlin视图“无法找到以下类”

我遇到了一个问题,包括XML布局文件中的自定义Kotlin视图。 代码如下: class CustomView: RelativeLayout { /** * Programmatic constructor */ constructor(aVariable: Boolean, context: Context): super(context) { … } /** * XML constructor */ @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0): super(context, attributeSet, defStyle) { … } } 和XML : <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" […]

Android 4.4中的自定义视图构造函数在Kotlin上崩溃,如何修复?

我有一个使用JvmOverloads写在Kotlin的自定义视图,我可以有默认值。 class MyView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyle: Int = 0, defStyleRes: Int = 0 ) : LinearLayout(context, attrs, defStyle, defStyleRes) 在Android 5.1及以上版本中,所有工作都很好。 然而它在4.4中崩溃,因为4.4中的构造函数没有defStyleRes 。 我怎么能支持5.1及以上的版本,我可以defStyleRes但不是在4.4,而不需要明确有4个构造函数定义像我们在Java中所做的? 注意:下面的4.4可以正常工作,但是我们defStyleRes了defStyleRes 。 class MyView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyle: Int = 0 ) : LinearLayout(context, attrs, defStyle)