You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android键盘行间距设置:如何为a/l键及键盘与文本添加间距?

Android自定义键盘布局问题解决方案

我来帮你搞定这两个自定义键盘的布局调整问题~

一、调整字符a和l的行内间距

你要调整的是键与键之间的间距还是字符在键内的左右间距?两种情况的解决方案如下:

情况1:增大a键与s键、k键与l键的间距

给目标键添加android:horizontalGap属性,这个属性用来设置当前键与前一个键的水平间距,数值可根据需求调整:

<Row>
    <Key android:codes="97" android:keyEdgeFlags="left" android:keyLabel="a" />
    <Key android:codes="115" android:keyLabel="s" 
         android:horizontalGap="8dp"/> <!-- 增大a和s的间距 -->
    <Key android:codes="100" android:keyLabel="d" />
    <Key android:codes="102" android:keyLabel="f" />
    <Key android:codes="103" android:keyLabel="g" />
    <Key android:codes="104" android:keyLabel="h" />
    <Key android:codes="106" android:keyLabel="j" />
    <Key android:codes="107" android:keyLabel="k" />
    <Key android:codes="108" android:keyEdgeFlags="right" android:keyLabel="l" 
         android:horizontalGap="8dp"/> <!-- 增大k和l的间距 -->
</Row>

情况2:调整a、l字符在键内的左右间距

如果是想让字符a/l和键的边缘之间留出更多空间,给对应的Key添加android:keyPaddingLeftandroid:keyPaddingRight属性:

<Row>
    <Key android:codes="97" android:keyEdgeFlags="left" android:keyLabel="a" 
         android:keyPaddingLeft="4dp" android:keyPaddingRight="4dp"/>
    <Key android:codes="115" android:keyLabel="s" />
    <Key android:codes="100" android:keyLabel="d" />
    <Key android:codes="102" android:keyLabel="f" />
    <Key android:codes="103" android:keyLabel="g" />
    <Key android:codes="104" android:keyLabel="h" />
    <Key android:codes="106" android:keyLabel="j" />
    <Key android:codes="107" android:keyLabel="k" />
    <Key android:codes="108" android:keyEdgeFlags="right" android:keyLabel="l" 
         android:keyPaddingLeft="4dp" android:keyPaddingRight="4dp"/>
</Row>

二、给键盘底部与文本之间添加间距

这里分两种场景,你可以根据实际需求选择:

场景1:键盘顶部与输入框(EditText)底部之间加间距

如果是输入框和键盘顶部贴合,给EditText添加底部外边距即可:

<EditText
    ...其他属性
    android:layout_marginBottom="8dp"/>

或者给KeyboardView添加顶部外边距:

<android.inputmethodservice.KeyboardView
    ...其他属性
    android:layout_marginTop="8dp"/>

场景2:键盘底部与屏幕底部之间加间距

如果是键盘底部和屏幕边缘贴合,直接在键盘的xml根标签<Keyboard>中添加底部内边距:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:bottomPadding="16dp"
    ...其他属性>
    <!-- 你的Row代码 -->
</Keyboard>

也可以在KeyboardView的布局中设置底部内边距或外边距:

<android.inputmethodservice.KeyboardView
    ...其他属性
    android:paddingBottom="16dp"/>

内容的提问来源于stack exchange,提问作者user9147798

火山引擎 最新活动