简体   繁体   English

如何在 android 中创建具有相同大小按钮的键盘

[英]how can I create a keyboard with buttons of identical size in android

I have a problem with creating a keyboard in android studio where every button has same size.我在 android 工作室中创建键盘时遇到问题,其中每个按钮的大小都相同。 I am trying to mimic the android keyboard that looks like this (without shift and delete button): desired keyboard我正在尝试模仿看起来像这样的 android 键盘(没有移位和删除按钮):所需的键盘

So far, my solution has been to create a vertical linear layout with three horizontal layouts inside of it.到目前为止,我的解决方案是创建一个包含三个水平布局的垂直线性布局。 Then i progmatically create buttons, give them a letter and put them in the correct horizontal layout.然后我以编程方式创建按钮,给它们一个字母并将它们放在正确的水平布局中。 It looks like this: my Keyboard它看起来像这样:我的键盘

As you can see, the buttons increase in size to fill the space.如您所见,按钮的大小会增加以填充空间。 However, i want all the buttons to have the same size, just like the android keyboard.但是,我希望所有按钮都具有相同的大小,就像 android 键盘一样。 this is my code so far:到目前为止,这是我的代码:

The vertical layout and 1 of the 3 horizontal layouts (they are all identical):垂直布局和 3 个水平布局中的 1 个(它们都是相同的):

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="20dp"
    app:layout_constraintBottom_toBottomOf="parent">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal"
        />

Button from java file: java 文件中的按钮:

for (int i = 0; i < 26; i++) {
        Button button = new Button(this);
        button.setOnClickListener(this);
        button.setMaxWidth(5);
        button.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        switch (i){
            case 0: button.setText("Q"); break;
            case 1: button.setText("W"); break;
            case 2: button.setText("E"); break;
            case 3: button.setText("R"); break;
            case 4: button.setText("T"); break;
            case 5: button.setText("Y"); break;
            case 6: button.setText("U"); break;
            case 7: button.setText("I"); break;
            case 8: button.setText("O"); break;
            case 9: button.setText("P"); break;
            case 10: button.setText("A"); break;
            case 11: button.setText("S"); break;
            case 12: button.setText("D"); break;
            case 13: button.setText("F"); break;
            case 14: button.setText("G"); break;
            case 15: button.setText("H"); break;
            case 16: button.setText("J"); break;
            case 17: button.setText("K"); break;
            case 18: button.setText("L"); break;
            case 19: button.setText("Z"); break;
            case 20: button.setText("X"); break;
            case 21: button.setText("C"); break;
            case 22: button.setText("V"); break;
            case 23: button.setText("B"); break;
            case 24: button.setText("N"); break;
            case 25: button.setText("M"); break;

        }
        if (i < 10){
            linearLayout1.addView(button);
        }else if (i < 19){
            linearLayout2.addView(button);
        }else{
            linearLayout3.addView(button);
        }
    }

I just create manually a keyboard with the help of nested LinearLayout, here is my XML code:我只是在嵌套 LinearLayout 的帮助下手动创建了一个键盘,这是我的 XML 代码:

<LinearLayout android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="35dp"
        android:paddingRight="35dp"
        >
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="Q"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="W"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="E"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="R"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="T"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="Y"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="U"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="I"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="O"/>

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="P"/>
    </LinearLayout>
        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="55dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingRight="55dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="A"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="S"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="D"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="F"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="G"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="H"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="J"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="K"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="L"/>

    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="150dp"
        android:paddingRight="75dp"
        android:paddingTop="10dp"
        android:paddingBottom="20dp"
        android:orientation="horizontal">

        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="Z"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="X"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="C"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="V"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="B"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="N"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="M"/>
    </LinearLayout>


</LinearLayout>

Output: Output:

Here KeyBoard UI is added这里添加了键盘 UI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM