kotlin中自定义TextView之drawableLeft与文本一起居中显示


import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.widget.TextView


/**
* Author: yangtianfu
* Date: 2021/1/25 18:08
* Describe:drawableLeft与文本一起居中显示
*/
@SuppressLint("AppCompatCustomView")
class DrawableCenterTextView : TextView {
    constructor(
        context: Context?, attrs: AttributeSet?,
        defStyle: Int
    ) : super(context, attrs, defStyle){}

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?) : super(context) {}

    override fun onDraw(canvas: Canvas) {
        val drawables = compoundDrawables
        if (drawables != null) {
            val drawableLeft = drawables[0]
            if (drawableLeft != null) {
                val textWidth = paint.measureText(text.toString())
                val drawablePadding = compoundDrawablePadding
                var drawableWidth =  drawableLeft.intrinsicWidth
                val bodyWidth = textWidth + drawableWidth + drawablePadding
                canvas.translate((width - bodyWidth) / 2, 0F)
            }
        }
        super.onDraw(canvas)
    }
}

使用

<DrawableCenterTextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="联系"
                android:drawablePadding="8dp"
                android:drawableLeft="@drawable/icon_call"
                android:textColor="#ff000000"
                android:textSize="14sp"/>
Kotlin 自定义键盘可以通过使用 Android 的 InputMethodService 类来实现。InputMethodService 是一个抽象类,它提供了创建自定义键盘的基本功能。以下是一个简单的示例: 1. 创建一个继承自 InputMethodService 的类,例如 CustomKeyboardService。 ```kotlin class CustomKeyboardService : InputMethodService() { // 实现自定义键盘的逻辑 } ``` 2. 在 AndroidManifest.xml 文件声明 CustomKeyboardService。 ```xml <service android:name=".CustomKeyboardService" android:label="Custom Keyboard" android:permission="android.permission.BIND_INPUT_METHOD"> <intent-filter> <action android:name="android.view.InputMethod" /> </intent-filter> <meta-data android:name="android.view.im" android:resource="@xml/method" /> </service> ``` 3. 创建一个 XML 文件用于定义键盘布局,例如 res/xml/method.xml。 ```xml <input-method xmlns:android="http://schemas.android.com/apk/res/android" android:settingsActivity=".CustomKeyboardSettingsActivity" /> ``` 4. 创建一个布局文件用于自定义键盘的界面,例如 res/layout/custom_keyboard.xml。 ```xml <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:horizontalGap="2px" android:verticalGap="2px"> <!-- 添加自定义的键盘按键 --> </Keyboard> ``` 5. 在 CustomKeyboardService 重写 onCreateInputView 方法,加载自定义键盘的布局。 ```kotlin override fun onCreateInputView(): View { val keyboardView = layoutInflater.inflate(R.layout.custom_keyboard, null) // 设置键盘按键的监听器等 return keyboardView } ``` 通过以上步骤,你可以创建一个简单的自定义键盘。你还可以通过监听按键事件、处理输入文本等来实现更复杂的功能。请注意,这只是一个简单示例,实际开发可能需要更多的逻辑和界面设计。 希望这个简单的示例对你有帮助!如果有任何疑问,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值