安卓软键盘的按键如何设置居中对齐

5
我一直在开发自定义键盘,但是我遇到了一个问题,无法解决。我试图将每行的所有按键都居中对齐。我的意思是,即使一行有9、10或11个按键,所有按键也都居中对齐。这是我的键盘目前的样子:http://postimg.org/image/6jy1uyd6x/。我做错了什么?这是我的xml文件:
    <?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
    android:horizontalGap="1%p"
    android:verticalGap="0.4%p"
    >
    <Row android:keyWidth="9%p" android:keyHeight="75dip" android:rowEdgeFlags="top">
        <Key android:codes="49" android:keyLabel=";" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="ς"/>
        <Key android:codes="51" android:keyLabel="ε"/>
        <Key android:codes="52" android:keyLabel="ρ"/>
        <Key android:codes="53" android:keyLabel="τ"/>
        <Key android:codes="54" android:keyLabel="υ"/>
        <Key android:codes="55" android:keyLabel="θ"/>
        <Key android:codes="56" android:keyLabel="ι"/>
        <Key android:codes="57" android:keyLabel="ο"/>
        <Key android:codes="48" android:keyLabel="π" android:keyEdgeFlags="right"/>
    </Row>

这是我的keyboard.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<android.inputmethodservice.KeyboardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/keyboard"
    android:imeOptions="flagNoFullscreen"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center|fill_horizontal"
    android:layout_alignParentBottom="true"
    android:keyPreviewLayout ="@layout/preview"
    android:keyBackground="@drawable/key_background"
    android:keyTextSize="35sp"
    android:keyTextColor="@color/key_text_color"
    android:keyPreviewHeight="50sp"
    android:shadowRadius="1"
    android:shadowColor="#fff"
    android:padding="0px" android:layout_margin="0px"
    tools:ignore="ResAuto" />

我不想使用%p,因为这不是解决此问题的最佳方式。 提前感谢您!
2个回答

3

尝试设置第一个键的 android:keyEdgeFlags="left" 属性,并将 android:horizontalGap="5%p" 添加到其中

<Row    >
        <Key android:codes="97" android:keyLabel="A" android:horizontalGap="5%p" 
                android:keyEdgeFlags="left"/>
        <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:keyLabel="L" android:keyEdgeFlags="right"/>
    </Row>

我尝试了这个,但我的键盘键仍然无法居中对齐。还有其他的想法吗?我仍在努力寻找这是如何发生的。在网站上搜索没有帮助我! - George
从键盘中删除 android:horizontalGap="1%p"。 - Zar E Ahmer
我的键盘仍然无法居中对齐。按键堆叠在一起,无法居中! - George
谢谢您的回答。我仍然无法弄清楚如何修复这个问题。这个解决方案对我没有起作用。 - George
我也遇到了同样的问题,而且layout_gravity不起作用!! - Morad Edwar
显示剩余2条评论

2
我找到了一个对我有用的解决方案。 Nepster给你的建议是正确的,除了我从最后一个字母中删除了keyEdgeFlags="right"。
<Row>
<key android:label="a" codes.... android:keyEdgeFlags="left" android:horizontalGap="5%p"/>
<key "s" .../>
<key "d" .../>
....
<key android:label="l" codes ... />

这给我提供了这个布局:

这只是我的一个编译示例


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接