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

问题描述 投票:0回答:1

我在android studio中创建键盘时遇到问题,其中每个按钮的大小均相同。我正在尝试模仿如下所示的android键盘(不带shift和Delete按钮):desired keyboard

到目前为止,我的解决方案是创建一个垂直的线性布局,并在其中放置三个水平布局。然后,我以编程方式创建按钮,给它们一个字母,然后将其放置在正确的水平布局中。看起来像这样:my Keyboard

如您所见,按钮的大小会增加以填充空间。但是,我希望所有按钮都具有相同的大小,就像android键盘一样。到目前为止,这是我的代码:

垂直布局和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"
        />

来自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);
        }
    }
android button layout
1个回答
0
投票

我只是在嵌套的LinearLayout的帮助下手动创建了KeyBoard,

这里是我的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>

输出:

Here KeyBoard UI is added

© www.soinside.com 2019 - 2024. All rights reserved.